[BaekJoon] #2720 - 세탁소 사장 동혁 [Java][C++]
[BaekJoon] #2720 - 세탁소 사장 동혁 [Java][C++]
1. 아이디어
거스름돈을 줄 때 동전의 개수가 최소가 되게 해야 하는 문제다. 동전의 액면가가 25, 10, 5, 1센트인 체계에서는 액면가가 더 큰 동전부터 최대한 많이 거슬러주면 동전의 개수가 최소가 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(T)$ | $O(1)$ |
($T$ = 테스트 케이스 수. Java는 전체 출력을 StringBuilder에 모았다가 출력하므로 공간 $O(T)$)
3. 코드
풀이 [Java][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
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int c = Integer.parseInt(br.readLine());
sb.append(c / 25).append(" ");
c %= 25;
sb.append(c / 10).append(" ");
c %= 10;
sb.append(c / 5).append(" ");
c %= 5;
sb.append(c).append("\n");
}
System.out.println(sb);
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int c;
cin >> c;
cout << c / 25 << ' ';
c %= 25;
cout << c / 10 << ' ';
c %= 10;
cout << c / 5 << ' ';
c %= 5;
cout << c << '\n';
}
}
This post is licensed under CC BY 4.0 by the author.