[BaekJoon] #2577 - 숫자의 개수 [Java][C++][Python]
[BaekJoon] #2577 - 숫자의 개수 [Java][C++][Python]
1. 아이디어
$A \times B \times C$는 최대 $999^3$으로 $10^9$ 미만이라 32비트 정수에 담기므로 곱을 그대로 구한 뒤 0부터 9까지 각 숫자가 몇 번 쓰였는지 센다.
2. 코드
풀이 [Java][C++][Python]
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 res = a * b * c;
int[] cnt = new int[10];
while (res > 0) {
cnt[res % 10]++;
res /= 10;
}
for (int x : cnt) {
System.out.println(x);
}
}
}
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 cnt[10];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a, b, c;
cin >> a >> b >> c;
int res = a * b * c;
while (res > 0) {
cnt[res % 10]++;
res /= 10;
}
for (int x : cnt) {
cout << x << '\n';
}
}
1
2
3
4
5
6
7
8
9
10
11
import sys
input = sys.stdin.readline
a = int(input())
b = int(input())
c = int(input())
s = str(a * b * c)
for d in range(10):
print(s.count(str(d)))
This post is licensed under CC BY 4.0 by the author.