[BaekJoon] 5585번 - 거스름돈 [Java][C++]
[BaekJoon] 5585번 - 거스름돈 [Java][C++]
1. 문제 풀이
$1000$ 엔 지폐 한장으로 $N$ 엔을 낸 잔돈의 최소 개수를 구하는 문제로 각 잔돈 액수들이 서로 배수, 약수 관계에 있어서 단위가 큰 동전부터 최대한 거슬러주면 된다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = 1000 - Integer.parseInt(br.readLine());
int cnt = 0;
cnt += N / 500;
N %= 500;
cnt += N / 100;
N %= 100;
cnt += N / 50;
N %= 50;
cnt += N / 10;
N %= 10;
cnt += N / 5;
N %= 5;
cnt += N;
System.out.println(cnt);
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
n = 1000 - n;
int cnt = 0;
cnt += n / 500;
n %= 500;
cnt += n / 100;
n %= 100;
cnt += n / 50;
n %= 50;
cnt += n / 10;
n %= 10;
cnt += n / 5;
n %= 5;
cnt += n;
cout << cnt;
}
This post is licensed under CC BY 4.0 by the author.