Guest User

Untitled

a guest
Dec 15th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. public static void waysMakeChangeDifferent(int centsToChange){
  2. coinDenoms = new int[]{25,10,5,1};
  3. int startSum = 0;
  4. int startDenom = 0;
  5. int[] startCoins = new int[coinDenoms.length];
  6. ArrayList<int[]> cc = waysMakeChangeDifferentRecurse(centsToChange, startSum, startDenom, startCoins);
  7.  
  8. System.out.println("There are "+
  9. cc.size()+
  10. " different solutions." );
  11.  
  12. for (int[] c: cc){
  13. for (int coin:c){
  14. System.out.print(coin+" ");
  15. }
  16. System.out.println();
  17. }
  18. }
  19. private static ArrayList<int[]> waysMakeChangeDifferentRecurse(int centsToChange, int currentSum, int currentDenom, int[] currentCoins){
  20. ArrayList<int[]> ret = new ArrayList<int[]>();
  21. for (int i = currentDenom; i<coinDenoms.length;i++){//Iterate through coins of current size or smaller
  22. int newSum = currentSum+coinDenoms[i];
  23. if (newSum>centsToChange){//If we pass the amount
  24. continue;
  25. }
  26. if (newSum==centsToChange){//If we hit the right amount
  27. currentCoins[i]++;
  28. ret.add(currentCoins);
  29. return ret;
  30. }
  31. //If we're still approaching the right amount
  32. int[] tempCoins = currentCoins.clone();
  33. tempCoins[i]++;
  34. ArrayList<int[]> differentWays = waysMakeChangeDifferentRecurse(centsToChange, newSum, i, tempCoins);
  35. if (differentWays!=null){
  36. ret.addAll(differentWays);
  37. }
  38. }
  39. return ret;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment