[BaekJoon] 2577번 - 숫자의 개수 [Java][C++]
[BaekJoon] 2577번 - 숫자의 개수 [Java][C++]
1. 문제 풀이
세 자연수의 곱을 구하고 각 자릿수를 세는 카운팅 배열을 활용하면 해결할 수 있다. 각 자릿수는 $10$ 으로 나눈 나머지를 구하고 몫만 남기는 과정을 반복하면 된다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int A = Integer.parseInt(br.readLine());
int B = Integer.parseInt(br.readLine());
int C = Integer.parseInt(br.readLine());
int result = A * B * C;
int[] cntArr = new int[10];
while (result > 0) {
cntArr[result % 10]++;
result /= 10;
}
for (int cnt : cntArr) {
System.out.println(cnt);
}
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b, c;
cin >> a >> b >> c;
int res = a * b * c;
int cntArr[10] = {};
while (res > 0) {
cntArr[res % 10]++;
res /= 10;
}
for (int cnt : cntArr) {
cout << cnt << '\n';
}
}
This post is licensed under CC BY 4.0 by the author.