Guest User

Untitled

a guest
Dec 10th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public static void waysMakeChangeDifferent(int centsToChange){
  2. coinCollections = new HashSet<CoinCollection>();
  3. System.out.println("There are "+waysMakeChangeDifferentRecurse(centsToChange, new CoinCollection())+" different solutions." );
  4. for (CoinCollection cc:coinCollections){
  5. System.out.println();
  6. System.out.println("Num pennies: "+cc.getNumPennies());
  7. System.out.println("Num nickels: "+cc.getNumNickels());
  8. System.out.println("Num dimes: "+cc.getNumDimes());
  9. System.out.println("Num quarters: "+cc.getNumQuarters());
  10. }
  11. }
  12. private static int waysMakeChangeDifferentRecurse(int centsToChange, CoinCollection inCoins){
  13. if (inCoins.getSum()>centsToChange){
  14. return 0;
  15. }
  16. if (inCoins.getSum()==centsToChange){
  17. coinCollections.add(inCoins);
  18. return 1;
  19. }
  20. //Check if any of the new coin sets have already been done
  21. int currQuarters = inCoins.getNumQuarters();
  22. int currDimes = inCoins.getNumDimes();
  23. int currNickels = inCoins.getNumNickels();
  24. int currPennies = inCoins.getNumPennies();
  25.  
  26. CoinCollection withNewQuarter = new CoinCollection(currPennies, currNickels, currDimes, currQuarters+1);
  27. int numFromQuarter = 0;
  28. if (!coinCollections.contains(withNewQuarter)){//If we haven't already found this value
  29. numFromQuarter = waysMakeChangeDifferentRecurse(centsToChange, withNewQuarter);
  30. }
  31.  
  32. CoinCollection withNewDime = new CoinCollection(currPennies, currNickels, currDimes+1, currQuarters);
  33. int numFromDime = 0;
  34. if (!coinCollections.contains(withNewDime)){
  35. numFromDime = waysMakeChangeDifferentRecurse(centsToChange, withNewDime);
  36. }
  37.  
  38. CoinCollection withNewNickel = new CoinCollection(currPennies, currNickels+1, currDimes, currQuarters);
  39. int numFromNickel = 0;
  40. if (!coinCollections.contains(withNewNickel)){
  41. numFromNickel = waysMakeChangeDifferentRecurse(centsToChange, withNewNickel);
  42. }
  43.  
  44. CoinCollection withNewPenny = new CoinCollection(currPennies+1, currNickels, currDimes, currQuarters);
  45. int numFromPenny = 0;
  46. if (!coinCollections.contains(withNewPenny)){
  47. numFromPenny = waysMakeChangeDifferentRecurse(centsToChange, withNewPenny);
  48. }
  49.  
  50. return numFromPenny+numFromNickel+numFromDime+numFromQuarter;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment