Results 1 to 8 of 8
Thread: Coin Change
- 09-22-2010, 04:47 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
- 09-22-2010, 05:04 PM #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.
- 09-22-2010, 06:48 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
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 #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.
- 09-23-2010, 07:18 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 09-23-2010, 05:09 PM #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.
- 09-23-2010, 05:28 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
No, I didn't mean that; here's the final code derived from a hypothetical 'schizophrenia' example:
I'm too lazy to come up with the actual example right now ;-)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); } } }
kind regards,
Jos
- 09-23-2010, 05:33 PM #8
Similar Threads
-
Java Coin Acceptor?
By starzsimon in forum Advanced JavaReplies: 5Last Post: 08-06-2011, 07:50 AM -
coin toss program(Eclipse)
By ccie007 in forum New To JavaReplies: 5Last Post: 08-06-2010, 07:03 PM -
Random coin flip application
By Boomer1 in forum New To JavaReplies: 8Last Post: 12-18-2009, 02:57 AM -
Is it possible to change the '\n' into ' ' ...
By johnny7white in forum New To JavaReplies: 1Last Post: 11-15-2007, 02:32 PM -
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11-11-2007, 08:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks