✔️문제
- 입력된 숫자의 개수를 출력하라.
- 입력: 421314218
- 정답:
0: 0
1: 3
2: 2
3: 1
4: 2
5: 0
6: 0
7: 0
8: 1
9: 0
📍정답
package _11_countNum;
public class Main {
public static void main(String[] args) {
int n = 421314218; // 추출->10으로 나누고 나머지(mode)
System.out.println(n);
int arr[] = new int[10]; // 0~9 입력된 수를 cnt 하는 용도
// int arr[0] = 0;
// int arr[1] = 3;
// int arr[4] = 2;
int temp = n; // 원본 n을 변경하지 않기 위해 임시 변수 사용
while (temp > 0) {
arr[temp % 10]++;
temp /= 10;
}
//출력력
for (int i = 0; i < 10; i++) {
if (arr[i] > 0) { // 해당 숫자가 존재할 경우만 출력
System.out.println(i + ": " + arr[i] + "번");
}
}
}
}
'개발인생 > Altorithm' 카테고리의 다른 글
[알고리즘 기초 100제] 13. 별찍기 (0) | 2025.03.11 |
---|---|
[알고리즘 기초 100제] 12. 구구단 (0) | 2025.03.11 |
[알고리즘 기초 100제] 10. 숫자 사각형(1)~(4) (1) | 2025.03.11 |
[알고리즘 기초 100제] 9. 각 자릿수 합 구하기 (2) | 2025.03.10 |
[알고리즘 기초 100제] 8. 팩토리얼 (0) | 2025.03.10 |