백준/그리디

거스름돈 - JAVA

연구하는개발자 2021. 3. 25. 18:45
728x90

www.acmicpc.net/problem/14916

 

14916번: 거스름돈

첫째 줄에 거스름돈 액수 n(1 ≤ n ≤ 100,000)이 주어진다.

www.acmicpc.net

package Greedy;

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

 class Change {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int expect = n / 5;
        int rest = n % 5;

        while (rest % 2 > 0) {
            if(expect ==0){
                System.out.println(-1);
                return;
            }
            expect--;
            rest = n - 5 * expect;
        }

        int answer = rest / 2 + expect;
        System.out.println(answer);
        return;
    }
}