https://www.acmicpc.net/problem/11047
이번 문제는 많이 풀어 본 문제 중 하나이다.
거스름돈의 동전 갯수를 출력하는 것과 같기 때문이다.
반복문 연습문제로도 자주 풀었던 문제였던 만큼 금방 풀 수 있었다.
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
public class Main {
static int count = 0;
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = br.readLine();
String[] nums = line.split(" ");
int n = Integer.parseInt(nums[0]);
int money = Integer.parseInt(nums[1]);
Integer coins[] = new Integer[n];
for (int i = 0; i < n; i++) {
String l = br.readLine();
coins[i] = Integer.parseInt(l);
}
int idx = 0;
Arrays.sort(coins, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
if (coins[i] <= money) {
count = count + money / coins[i];
money = money % coins[i];
}
}
bw.write(Integer.toString(count));
br.close();
bw.close();
}
}