알고리즘

백준 5585번 거스름돈 (JAVA)

박카스마시며코딩 2023. 6. 30. 11:08

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

 

5585번: 거스름돈

타로는 자주 JOI잡화점에서 물건을 산다. JOI잡화점에는 잔돈으로 500엔, 100엔, 50엔, 10엔, 5엔, 1엔이 충분히 있고, 언제나 거스름돈 개수가 가장 적게 잔돈을 준다. 타로가 JOI잡화점에서 물건을 사

www.acmicpc.net

 

저는 그리디를 이용해 문제를 해결하였습니다.

가장 큰 돈 단위부터 돈을 나눠주면서 잔돈의 개수를 확인하였습니다

 

package BOJ.greedy;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.function.Function;

public class BOJ_5585 {
    private static final int[] MONEY = {500,100,50,10,5,1};
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(br.readLine());
        int result = cal(1000 - n);
        System.out.println(result);
    }
    private static int cal(int n){
        int result = 0;
        for(int money : MONEY){
            result += n / money;
            n %= money;
        }
        return result;
    }
}