Java/SW Expert Academy

1970. 쉬운 거스름돈

GLOWWW 2021. 1. 9. 22:03
package oSWExpertAcademy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class D2_1970 {
    public static void main(String[]args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            int tc = Integer.parseInt(br.readLine());
            for(int c=1; c<=tc; c++) {
                //거스름돈 입
                int exchange = Integer.parseInt(br.readLine());
                //잔돈 종류의 갯수 배열 생
                int[] count = new int[8];
                //금액 단위 배열  
                int[] money = {50000, 10000, 5000, 1000, 500, 100, 50, 10};
                for(int i=0; i<count.length; i++) {
                    //money    배열을 돌려 거스름돈이 금액 단위 배열 보다 클경우. 
                    if(exchange >= money[i]) {
                        //거스름돈을 해당 금액 단위로 나눠서 몫을 구한다. 
                        int change = exchange/money[i];
                        //몫은 잔돈의 갯수에 들어간다. 
                        count[i] = change;
                        //그 갯수와 금액단위배열을 곱하여 거스름돈에서 빼 나가면서 점점 작은 단위로 나아간다. 
                        exchange -= change*money[i];
                    }
                }
                System.out.println("#"+c);
                for(int i=0; i<8; i++) {
                System.out.print(count[i]+" ");
                }
                System.out.println();
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}