PS/BOJ

백준 5585 거스름돈 / JAVA

얍연구소장 2023. 4. 19.

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);
	}
}

'PS > BOJ' 카테고리의 다른 글

백준 1676 팩토리얼 0의 개수 / JAVA  (0) 2023.05.03
백준 1343 폴리오미노  (0) 2023.04.20
백준 1331 나이트투어 / JAVA  (0) 2023.04.19
백준 9465 스티커 / JAVA  (0) 2023.04.17
백준 1251 단어 나누기 / JAVA  (0) 2023.04.14

댓글