배열뒤에서 부터 계산해주면 편하다.
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
|
package greedy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main5585_거스름돈 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int change = Integer.parseInt(br.readLine());
int[] value = { 1, 5, 10, 50, 100, 500 };
int money = 1000 - change;
int count = 0;
for (int i = value.length - 1; i >= 0; i--) {
int num = money / value[i];
money = money - (value[i] * num);
count += num;
if(money ==0) break;
}
System.out.println(count);
}//end of main
}//end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
'Algorithm 문제풀이 > greedy' 카테고리의 다른 글
[BOJ] 백준 10610 - 30 (JAVA) (0) | 2019.06.19 |
---|---|
[BOJ] 백준 2875 - 대회or인턴 (JAVA) (0) | 2019.06.19 |
[BOJ] 백준 1931 - 회의실배정 (JAVA) (0) | 2019.06.18 |
[BOJ] 백준 11047 - 동전 (JAVA) (0) | 2019.06.16 |
[BOJ] 백준 11399 - ATM (JAVA) (0) | 2019.06.16 |