Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void waysMakeChangeDifferent(int centsToChange){
- coinDenoms = new int[]{25,10,5,1};
- int startSum = 0;
- int startDenom = 0;
- int[] startCoins = new int[coinDenoms.length];
- ArrayList<int[]> cc = waysMakeChangeDifferentRecurse(centsToChange, startSum, startDenom, startCoins);
- System.out.println("There are "+
- cc.size()+
- " different solutions." );
- for (int[] c: cc){
- for (int coin:c){
- System.out.print(coin+" ");
- }
- System.out.println();
- }
- }
- private static ArrayList<int[]> waysMakeChangeDifferentRecurse(int centsToChange, int currentSum, int currentDenom, int[] currentCoins){
- ArrayList<int[]> ret = new ArrayList<int[]>();
- for (int i = currentDenom; i<coinDenoms.length;i++){//Iterate through coins of current size or smaller
- int newSum = currentSum+coinDenoms[i];
- if (newSum>centsToChange){//If we pass the amount
- continue;
- }
- if (newSum==centsToChange){//If we hit the right amount
- currentCoins[i]++;
- ret.add(currentCoins);
- return ret;
- }
- //If we're still approaching the right amount
- int[] tempCoins = currentCoins.clone();
- tempCoins[i]++;
- ArrayList<int[]> differentWays = waysMakeChangeDifferentRecurse(centsToChange, newSum, i, tempCoins);
- if (differentWays!=null){
- ret.addAll(differentWays);
- }
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment