Post

[BOJ 2577] 숫자의 개수

Baekjoon Online Judge 2577(Java 11)
[숫자의 개수] 문제 풀이

[BOJ 2577] 숫자의 개수

-> 문제 바로가기



시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초128 MB21674613297310859060.957%

문제


  • 세 개의 자연수 A, B, C가 주어질 때 A × B × C를 계산한 결과에 0부터 9까지 각각의 숫자가 몇 번씩 쓰였는지를 구하는 프로그램을 작성하시오.

  • 예를 들어 A = 150, B = 266, C = 427 이라면 A × B × C = 150 × 266 × 427 = 17037300 이 되고, 계산한 결과 17037300 에는 0이 3번, 1이 1번, 3이 2번, 7이 2번 쓰였다.


입력


  • 첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.


출력


  • 첫째 줄에는 A × B × C의 결과에 0 이 몇 번 쓰였는지 출력한다. 마찬가지로 둘째 줄부터 열 번째 줄까지 A × B × C의 결과에 1부터 9까지의 숫자가 각각 몇 번 쓰였는지 차례로 한 줄에 하나씩 출력한다.


예제


1
2
3
4
// 입력
150
266
427
1
2
3
4
5
6
7
8
9
10
11
// 출력
3
1
0
2
0
0
0
2
0
0


출처



알고리즘 분류








제출



내 제출


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
import java.io.*;
import java.util.*;

public class Main {

    public static void solution() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine()) * Integer.parseInt(br.readLine()) * Integer.parseInt(br.readLine());
        String multiply = (String.valueOf(n)); // 인덱스 추출을 위해 casting

        int[] count = new int[10];

        int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;

        char[] arr = multiply.toCharArray();

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < count.length; j++) {
                if ((arr[i] - '0') == j) { // char형을 int로 바꾸기 위해 "- '0'" 사용
                    count[j]++;
                }
            }
        }
        for (int i = 0; i < count.length; i++) {
            bw.write(count[i] + "\n");
        }


        bw.flush();
        bw.close();
        br.close();
    }


    public static void main(String[] args) throws IOException {
        solution();
    }
}


런타임메모리
120 ms15768 KB


Reference


This post is licensed under CC BY 4.0 by the author.