-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgo_11.java
More file actions
46 lines (34 loc) · 995 Bytes
/
Copy pathAlgo_11.java
File metadata and controls
46 lines (34 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package algo02;
import java.util.Arrays;
// 숫자의 갯수 구하기
public class Algo_11 {
public static String getNum(int num) {
int[] a = new int[10];
// 입력 num 을 String 으로 변환 배열로 변환
String[] getNumArray = String.valueOf(num).split("");
for(int i = 0; i < getNumArray.length; i++) {
for(int j = 0; j < 10; j++) {
if( Integer.parseInt(getNumArray[i]) == j ) {
a[j] += 1;
}
}
}
return Arrays.toString(a);
}
public static void main(String[] args) {
// 입력 421314218 --> 0:0, 1:3, 2:2, 3:1, 4:2, 5:0, 6:0, 7:0, 8:1, 9:0
System.out.println(getNum(421314218));
// 정답
int n = 421314218;
int [] arr = new int [10]; // 0 ~ 9 입력된 수를 cnt 위한 용도
// 10 으로 나눈 나머지를 추출
while( n > 0 ) {
// 해당하는 인덱스를 1씩 증가 시켜줌
arr[n % 10]++;
n /= 10;
}
for(int i=0; i < 10; i++) {
System.out.println( i + " : " + arr[i] );
}
}
}