Algorithm

[백준] 2577번 - 숫자의 개수

whdvlf94 2020. 3. 23. 16:31

문제

풀이

import java.io.*;

public class ArrayEx2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = 1;
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        for (int i = 0; i < 3; i++) {
            int x = Integer.parseInt(br.readLine());
            num = num * x;
        }
        br.close();
        System.out.println(num);    // 입력한 3개의 값을 모두 곱한 값


        for (int i = 0; i < array.length; i++) {
            int count = 0;
            int temp = num;
            while (temp != 0) {    // array의 인덱스 값이 변하는 것이 아닌, temp 값을 변화시켜 array와 비교한다.
                if (temp % 10 == array[i]) {    // 끝의 자리를 비교하기 위해 나머지 값을 이용한다.
                    count++;
                }
                temp /= 10;    // 위에서 이미 비교한 값을 없애기 위해 몫만 남긴다.
            }
         System.out.printf("[%s]:%s개\n",i,count);
        }
    }
}

출력 결과