[JAVA] 백준 : 숫자의 개수

조예빈·2024년 8월 3일
0

Coding Test

목록 보기
90/138

https://www.acmicpc.net/problem/2577

배열의 index에 각 숫자의 번호를 넣고, 1씩 증가시켜주면 되는 문제이다. 이 문제에서는 형 변환이 중요하다. A,B,C를 곱한 값을 String으로 변환해 length를 구해주고, 또 charAt을 통해 추출한 문자를 int로 변환해 index에 넣어 주면 된다.

package bronze2;

import java.util.Scanner;

public class num2577 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();

        int product = A * B * C;
        String proStr = product + "";
        int[] nums = new int[10]; //0부터 9까지 숫자 빈도를 저장

        for (int i = 0; i < proStr.length(); i++) {
            int digit = proStr.charAt(i) - '0';
            nums[digit]++;
        }
        for (int i = 0; i <= 9; i++) {
            System.out.println(nums[i]);
        }
        sc.close();
    }
}

profile
컴퓨터가 이해하는 코드는 바보도 작성할 수 있다. 사람이 이해하도록 작성하는 프로그래머가 진정한 실력자다. -마틴 파울러

0개의 댓글