728x90

핵심 키워드

동전, 최소

 

문제 요약

손님에게 거스름돈을 줄 때 동전의 개수를 최소로 하는 경우를 계산

그러기 위해선 가장 큰 단위의 동전부터 계산해야 함

 

문제 풀이

public class Main {
    public static void main(String[] args) {
        int[] charges = {25, 10, 5, 1};
        String[] count = new String[4];

        Scanner s = new Scanner(System.in);
        int test = s.nextInt();

        for(int i = 0; i < test; i++) {
            int cent = s.nextInt();

            count[0] = String.valueOf(cent/charges[0]);
            cent%=charges[0];
            count[1] = String.valueOf(cent/charges[1]);
            cent%=charges[1];
            count[2] = String.valueOf(cent/charges[2]);
            cent%=charges[2];
            count[3] = String.valueOf(cent/charges[3]);

            System.out.println(String.join(" ", count));
        }
    }
}

우선 동전의 종류 4가지를 배열에 저장할 때

단위는 입력의 단위에 맞춰서 저장한다. (0.25 >> 25)

 

테스트케이스의 수만큼 입력을 받고

이를 가장 큰 단위의 동전부터 거스름돈을 계산한다.

 

각각의 동전의 수를 출력한다.

+ Recent posts