import java.util.*;
class Coin {
public String name;
public double value;
public int count;
Coin(String itemName, double value) {
this.name = itemName;
this.value = value;
this.count = 0;
}
Coin(String name, double value, int count) {
this.name = name;
this.value = value;
this.count = count;
}
public static class myComparator implements Comparator<Coin>{
public int compare(Coin o1,Coin o2){
return (int)(o2.value - o1.value);
}
}
public static LinkedList<Coin> changeMaking(int totalValue,Coin[] coins){
LinkedList<Coin> coin = new LinkedList<>();
myComparator myComp = new myComparator();
Arrays.sort(coins, myComp);
int leftChange = totalValue;
for(int i = 0; i < coins.length; i++){
if(leftChange >= coins[i].value){
int coinNeed = (int)(leftChange/coins[i].value);
leftChange -= coinNeed * coins[i].value;
coin.add(new Coin(coins[i].name,coins[i].value,coinNeed));
}else{
continue;
}
}
return coin;
}
public static void main(String[] args) {
Coin[] coins = {
new Coin("二角五分硬币", 25),
new Coin("一角硬币", 10),
new Coin("五分硬币", 5),
new Coin("一分硬币", 1)
};
LinkedList<Coin> coinMaking = changeMaking(48, coins);
System.out.println("用如下硬币找零48美分:");
for (int i = 0; i < coinMaking.size(); ++i) {
System.out.println(coinMaking.get(i).name + " " + coinMaking.get(i).count);
}
}
}