/* "Program 8" * Example: Change for change * RMB has 100, 50, 10, 5, 2, 1, 0.5, 0.2, 0.1, and other denominations (unit for Yuan). * In the case of change, there are a variety of options, such as the need to make a change of 68.90 yuan, at least the following options: * 1 50, 1 10, 1 sheets 5, 3 1, 1 0.5, 2 sheets 0.2; * 1 50, 1 10, 1 5, 3 1, 1 0.5, 4 sheets 0.1; * 6 10, 1 5,
3 sheets 1, 1 0.5, 2 sheets 0.2.
* Using greedy algorithm for make change scheme * * * import Java.util.Scanner; public class T009 {public static void Exchange (float n) {int[] Parvalue = {10000,5000,2000,1000,500,200,100,50,20,10}; Par value (*100) int[] sum = new Int[parvalue.length]; The quantity used to record each face value int i; Record the position of the maximum value for (i=0;i<parvalue.length;i++) {if (N>parvalue[i]) {break;//Get I Value}} while (N>0&am
P;&i<parvalue.length) {//greedy algorithm if (N>=parvalue[i]) {n-= parvalue[i];
sum[i]++;
}else if (n<10&&n>=5) {sum[i]++;
Break
}else{i++; for (int k=0; k<parvalue.length;k++) {if (sum[k]!=0) {System.out.println (float) parvalue[k]/100+ "T yuan change composition
"+sum[k]+");
}} public static void Main (string[] args) {Scanner scan = new Scanner (system.in); System. Out.print ("Enter change Amount:");
float n = scan.nextfloat ();
Exchange (N*100);
}
}
Run Result: