본문 바로가기
알고리즘/백준

[백준 2577번 ː 자바(JAVA)] 숫자의 개수

by 그릿er 2020. 4. 27.

 

<문제>

 

세 개의 자연수 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번 쓰였다.

 

 

 

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
import java.util.*;
 
 
public class Main {
 
    public static void main(String[] args)  {
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        sc.close();
        
        int tot = a*b*c;
        
        int[] num = new int[10];
        int n;
        int i = 10;
        
        while(tot/i!=0){
            n = tot%i;
            num[n]++;
            tot = tot/i;
        }
        
        n = tot;
        num[n]++;
            
        for(int j=0; j<num.length; j++) {
            System.out.println(num[j]);
        }
        }
    }