문제
- 입력된 숫자의 개수를 출력하시오
입력 : 4214215218
결과
0: 0
1: 2
2: 3
3: 0
4: 2
5: 1
6: 0
7: 0
8: 1
9: 0
- 중첩반복문 이용, 배열
보완할 점
소스코드
package Algorithm;
import java.util.Scanner;
public class Algorithm13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num = sc.next(); //글자수 세기 위해서 String으로 선언
int[] arr = new int[10];
for(int i = 0; i < num.length(); i++) { //입력한 숫자
for(int j = 0; j < arr.length; j++) { // 배열
if((num.charAt(i) - '0') == j) {
arr[j] += 1; //+1씩 해당배열에 저장
}
}
}
//출력
for(int i = 0; i < arr.length; i++) {
System.out.println(i + ": " + arr[i]);
}
}
}
'알고리즘 문제연습 > 기초 알고리즘' 카테고리의 다른 글
[016] 별 찍기 (하나씩 증가) (0) | 2021.05.29 |
---|---|
[015] 구구단 출력하기 (0) | 2021.05.29 |
[014] 숫자 사각형 출력하기(행 자릿수 만큼 증가) (0) | 2021.05.26 |
[013] 숫자 사각형 출력하기(세로로) (0) | 2021.05.26 |
[012] 숫자 사각형 출력하기(거꾸로) (0) | 2021.05.26 |
댓글