// A program to determine the minimum number of coins needed to make change. // (c) 1997 duane a. bailey import structure.*; public class FullPostage { static int count1, count2; //+countImpl public static int stampCount(int amount) // pre: amount >= 0 // post: return *number* of stamps needed to make change // (only use 1 cent, 20 cent, and 32 cent stamps) { return stampCount(amount, new int[amount+1]); } protected static int stampCount(int amount, int answer[]) // pre: amount >= 0; answer array has length >= amount // post: return *number* of stamps needed to make change // (only use 1 cent, 20 cent, and 32 cent stamps) { int minStamps; Assert.pre(amount >= 0,"Reasonable amount of change."); if (amount == 0) return 0; if (answer[amount] != 0) return answer[amount]; // consider use of a penny stamp minStamps = 1+stampCount(amount-1,answer); // consider use of a 20 cent stamp if (amount >= 20) { int possible = 1+stampCount(amount-20,answer); if (minStamps > possible) minStamps = possible; } // consider use of a 32 cent stamp if (amount >= 32) { int possible = 1+stampCount(amount-32,answer); if (minStamps > possible) minStamps = possible; } answer[amount] = minStamps; return minStamps; } //-countImpl public static void main(String args[]) { for (int amount = 1; amount < 100; amount++) { count1=count2 = 0; System.out.println("Stamps returned to make "+amount+" cents: "+ stampCount(amount)); System.out.println(count1+" total "+count2+" nontrivial"); } } static Vector amounts; public static coins change(int amount) { Assert.pre(amount >= 0, "Amount must be positive."); amounts.ensureCapacity(amount+1); if (amounts.elementAt(amount) == null) { coins best, possible; if (amount == 0) best = new coins(); else { possible = change(amount-1).addPenny(); best = possible; if (amount >= 5) { possible = change(amount-5).addNickle(); if (best.total() > possible.total()) best = possible; } if (amount >= 10) { possible = change(amount-10).addDime(); if (best.total() > possible.total()) best = possible; } if (amount >= 25) { possible = change(amount-25).addQuarter(); if (best.total() > possible.total()) best = possible; } } amounts.setElementAt(best,amount); } return new coins((coins)amounts.elementAt(amount)); } /* public static void main(String args[]) { amounts = new Vector(); for (int i = 0; i < 100; i++) { System.out.println("Change for "+i+" cents: "+change(i)); } }*/ } class coins { int pennies, nickles, dimes, quarters; public coins() { pennies = nickles = dimes = quarters = 0; } public coins(coins that) { this.pennies = that.pennies; this.nickles = that.nickles; this.dimes = that.dimes; this.quarters = that.quarters; } public coins addPenny() { pennies++; return this;} public coins addNickle() { nickles++; return this;} public coins addDime() { dimes++; return this;} public coins addQuarter() { quarters++; return this;} public int total() { return pennies+nickles+dimes+quarters; } public String toString() { return "pennies: "+pennies+", nickles: "+nickles+ ", dimes: "+dimes+", quarters: "+quarters; } }