1. Member
Join Date
Jul 2010
Posts
11
Rep Power
0

## Coin Change

Figured it out.
Last edited by Growler; 11-30-2010 at 08:13 AM.

2. 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.

3. 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

4. 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.

5. 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

6. 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.

7. 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:

Java 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

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

Java 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. ;)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•