import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
public class ChangeTemplate {
public static class Coin {
public String name;
public int value;
public int count;
public Coin(String name, int value, int count) {
this.name = name;
this.value = value;
this.count = count;
}
}
public static ArrayList<Coin> makeChange(int amount, Coin[] coins) {
ArrayList<Coin> result = new ArrayList<Coin>();
Arrays.sort(coins, new Comparator<Coin>() {
public int compare(Coin c1, Coin c2) {
return c2.value - c1.value;
}
});
for (int i = 0; i < coins.length; i++) {
Coin coin = coins[i];
while (amount >= coin.value && coin.count > 0) {
amount -= coin.value;
coin.count--;
result.add(new Coin(coin.name, coin.value, 1));
}
}
if (amount != 0) {
result.clear();
}
return result;
}
public static void main(String[] args) {
Coin[] coins = {
new Coin("1元", 100, 5),
new Coin("五角", 50, 3),
new Coin("二角五分", 25, 2),
new Coin("一角", 10, 2),
new Coin("五分", 5, 1),
new Coin("一分", 1, 10)
};
int amount = 183;
ArrayList<Coin> result = makeChange(amount, coins);
if (result.isEmpty()) {
System.out.println("无法找零");
} else {
System.out.println("找零如下:");
for (Coin coin : result) {
System.out.println(coin.name);
}
}
}
}