Results 1 to 8 of 8

Thread: Coin Change

  1. #1
    Growler is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Coin Change

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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    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. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    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. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    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. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    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. ;)

Similar Threads

  1. Java Coin Acceptor?
    By starzsimon in forum Advanced Java
    Replies: 5
    Last Post: 08-06-2011, 07:50 AM
  2. coin toss program(Eclipse)
    By ccie007 in forum New To Java
    Replies: 5
    Last Post: 08-06-2010, 07:03 PM
  3. Random coin flip application
    By Boomer1 in forum New To Java
    Replies: 8
    Last Post: 12-18-2009, 02:57 AM
  4. Is it possible to change the '\n' into ' ' ...
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 02:32 PM
  5. Replies: 2
    Last Post: 11-11-2007, 08:07 AM

Posting Permissions

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