Skip to content

Commit 9e28db0

Browse files
committed
[Silver V] Title: 거스름돈, Time: 168 ms, Memory: 17708 KB -BaekjoonHub
1 parent 0178e56 commit 9e28db0

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Silver V] 거스름돈 - 14916
2+
3+
[문제 링크](https://www.acmicpc.net/problem/14916)
4+
5+
### 성능 요약
6+
7+
메모리: 17708 KB, 시간: 168 ms
8+
9+
### 분류
10+
11+
수학, 다이나믹 프로그래밍, 그리디 알고리즘
12+
13+
### 제출 일자
14+
15+
2026년 3월 4일 22:24:52
16+
17+
### 문제 설명
18+
19+
<p>춘향이는 편의점 카운터에서 일한다.</p>
20+
21+
<p>손님이 2원짜리와 5원짜리로만 거스름돈을 달라고 한다. 2원짜리 동전과 5원짜리 동전은 무한정 많이 가지고 있다. 동전의 개수가 최소가 되도록 거슬러 주어야 한다. 거스름돈이 n인 경우, 최소 동전의 개수가 몇 개인지 알려주는 프로그램을 작성하시오.</p>
22+
23+
<p>예를 들어, 거스름돈이 15원이면 5원짜리 3개를, 거스름돈이 14원이면 5원짜리 2개와 2원짜리 2개로 총 4개를, 거스름돈이 13원이면 5원짜리 1개와 2원짜리 4개로 총 5개를 주어야 동전의 개수가 최소가 된다.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 거스름돈 액수 n(1 ≤ n ≤ 100,000)이 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>거스름돈 동전의 최소 개수를 출력한다. 만약 거슬러 줄 수 없으면 -1을 출력한다.</p>
32+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
int input = sc.nextInt();
7+
8+
int count = 0;
9+
10+
int fiveHundredWon = (int) input / 5;
11+
int exchange = input - 5 * fiveHundredWon;
12+
13+
if (input == 3 || input == 1) {
14+
System.out.println(-1);
15+
return;
16+
}
17+
18+
if (exchange % 2 == 0) {
19+
if (exchange == 0) {
20+
System.out.println(fiveHundredWon);
21+
return;
22+
}
23+
24+
count += fiveHundredWon;
25+
count += (int) exchange / 2;
26+
System.out.println(count);
27+
}
28+
29+
if (exchange % 2 == 1) {
30+
count += fiveHundredWon - 1;
31+
count += (int) (exchange + 5) / 2;
32+
System.out.println(count);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)