PS/BOJ

백준 5585 거스름돈 / JAVA

얍연구소장 2023. 4. 19. 22:40

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

 

5585번: 거스름돈

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

www.acmicpc.net

 

 

> 풀이

 

import java.util.Scanner;

/*
 * 백준 5585 거스름돈
 * #브론즈2
 * #그리디
 */
public class boj_5585 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int price = sc.nextInt();
		
		int[] n = {500, 100, 50, 10, 5, 1};
		int bill = 1000;
		
		solution(price, bill, n);
	}

	public static void solution(int price, int bill, int[] n) {
		int answer = 0; // 동전 갯수
		int changes = bill - price; // 잔액
		for(int coin : n) {
			if(changes >= coin) {
				answer += changes / coin;
				changes %= coin;
			}
		}
		System.out.println(answer);
	}
}
댓글수0