# Coin Change

• 09-22-2010, 04:47 PM
Growler
Coin Change
Figured it out.
• 09-22-2010, 05:04 PM
Norm
For good suggestions, you'll need to post the code that shows where the problem is.
Also add some comments in the code describing what you want it to do.
• 09-22-2010, 06:48 PM
JosAH
My schizophrenia example applies here again: I can do the quarters while my intelligent friend can do all the solutions involving dimes, nickles and pennies.

kind regards,

Jos
• 09-22-2010, 11:12 PM
Zack
That's not really recursive though, JosAH. You have one function to handle the quarters, which calls one to handle dimes, then one for nickels... or else it's all called by one function. There is no recursive call necessary using that method.
• 09-23-2010, 07:18 AM
JosAH
Quote:

Originally Posted by Zack
That's not really recursive though, JosAH. You have one function to handle the quarters, which calls one to handle dimes, then one for nickels... or else it's all called by one function. There is no recursive call necessary using that method.

Obviously you have never read one of my other schizophrenia examples ;-) That friend of mine is me so the example ends up as recursive as they come.

kind regards,

Jos
• 09-23-2010, 05:09 PM
Zack
I have read one of them actually--and I understood it perfectly in that case. In this cause, though, you're saying that each time you're submitting to a different function--or else one function has to have boolean operators telling it whether or not to handle each of the four types of coinage.

As a bit of an example, you cannot call getChange(50) once, then in that method, attempt it with two quarters, then attempt it with one quarter and run getChange(25) again. Otherwise it will come back with a single quarter, which you already have. As you get higher and more complex values, you will end up with more and more duplicate values if you use a recursive method in the form you described.
• 09-23-2010, 05:28 PM
JosAH
Quote:

Originally Posted by Zack
I have read one of them actually--and I understood it perfectly in that case. In this cause, though, you're saying that each time you're submitting to a different function--or else one function has to have boolean operators telling it whether or not to handle each of the four types of coinage.

As a bit of an example, you cannot call getChange(50) once, then in that method, attempt it with two quarters, then attempt it with one quarter and run getChange(25) again. Otherwise it will come back with a single quarter, which you already have. As you get higher and more complex values, you will end up with more and more duplicate values if you use a recursive method in the form you described.

No, I didn't mean that; here's the final code derived from a hypothetical 'schizophrenia' example:

Code:

```import java.util.Arrays; public class T {         private static final int[] coins= { 25, 10, 5, 1 };                 public static void main(String[] args) {                                 distribute(237, 0, new int[coins.length]);         }                 private static void distribute(int sum, int i, int[] amount) {                                 if (i == amount.length-1) {                         amount[i]= sum;                         System.out.println(Arrays.toString(amount));                 }                 else                         for (amount[i]= 0; amount[i]*coins[i] <= sum; amount[i]++) {                                 distribute(sum-amount[i]*coins[i], i+1, amount);                         }         } }```
I'm too lazy to come up with the actual example right now ;-)

kind regards,

Jos
• 09-23-2010, 05:33 PM
Zack
Quote:

Originally Posted by JosAH
No, I didn't mean that; here's the final code derived from a hypothetical 'schizophrenia' example:

Code:

`...`
I'm too lazy to come up with the actual example right now ;-)

kind regards,

Jos

Alright, I see what you're getting at. It sounded like you were just re-calling the same function with smaller values each time. ;)