본문 바로가기
Problem Solving/Baekjoon

[백준] 27106 Making Change - Greedy / Java

by graycode 2024. 4. 11.

 문제 링크

 

27106번: Making Change

Given the amount of a purchase (1 ≤ P ≤ 99) in cents, determine the way to make "change for a dollar" for that purchase. Use four standard US coin denominations: 1, 5, 10, and 25. The way to make change uses the least number coins.

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int p = 100 - read();
        sb.append(p / 25).append(" ").append((p %= 25) / 10).append(" ").append((p %= 10) / 5).append(" ").append(p % 5);

        bw.write(sb.toString());
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글