Results 1 to 7 of 7
  1. #1
    Luisgur is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Lightbulb some ideas for how to confront this problem?

    So i want to make a program that returns boolean. The user inputs 4 numbers, and then 1 more, the program should return true if using all 4 numbers, adding, subtracting, multipling and dividing you can form the 5th number, example:

    [3,6,1,11], [5]

    11-1 = 10
    6/3 = 2

    10/2 = 5-----> True

    my question is if anyone have any idea how to make this program? recursion? many for´s?
    Thanks!

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,988
    Rep Power
    5

    Default Re: some ideas for how to confront this problem?

    What would you recurse on?

    Why MANY for's? For now I read only one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,599
    Rep Power
    5

    Default Re: some ideas for how to confront this problem?

    Are you asking how to repetively prompt for user input (easy) or how to determine success or failure of the supplied input (much harder)?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: some ideas for how to confront this problem?

    I don't really want to give you the answer. But, I'll give you the format for your function that will work, and I'll give a link to where I learned to do these. They are very easy once you grasp the concept of doing them. The link is: CodingBat Java Recursion-1

    coding bat is an excellent website to learn specific techniques like this. Go through recursion 1 (which I linked you to) so you understand how recursion works.

    Then, go through recursion 2 - it'll teach you how to actually do your problem.

    You'll want to have your recursive function start with something like this:

    Java Code:
    public boolean canMake(int start, double[] nums, double target, double tot)
    Nums[] will hold your numbers. Start is where you start at in your number array so that you get all of the combos. Target will be what number you want to get.

    By sending back a bunch of different branches, since you have so many options, and then changing the start index in each branch and doing tot - nums[start], another branch with tot + nums[start], *, and /, then changing start, then returning true if tot == target, false if start >= nums.length, you'll have a working function. You also have to make sure you start a set of branches starting with just the next number, without using it. AKA, canMake( start + 1, nums, target, tot)


    EDIT: For the other posts, I think I understand what he is asking as I've done the same problem at one point. Given a couple of numbers, find if any number of them can be combined in any way to get the target number. It MUST be recursion. It's a tricky problem until you understand recursion
    Last edited by AlexGraal; 01-21-2014 at 05:37 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: some ideas for how to confront this problem?

    Quote Originally Posted by AlexGraal View Post
    It MUST be recursion.
    No it doesn't, because what those silly computers call recursion, actually is iteration in diguise; finite stacks can be very well emulated with arrays ... to iterate is human, recursion is divine.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: some ideas for how to confront this problem?

    All right. For an elegant solution, it must be recursion. I guess there are other ways to do it. I just wrote it up really quickly and it is 11 lines in total , including main method and everything. The entire program. And it works.

    I don't think you could write this in 11 lines without using recursion

    in fact, the method itself can be as short as 3 lines.
    Last edited by AlexGraal; 01-21-2014 at 07:58 PM.

  7. #7
    Luisgur is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: some ideas for how to confront this problem?

    Thanks a lot Alex! if i have any problem i will be asking you, im new at this so bear with me. :D

Similar Threads

  1. GUI ideas
    By Ruben in forum Forum Lobby
    Replies: 2
    Last Post: 06-24-2012, 04:19 AM
  2. Replies: 8
    Last Post: 11-05-2010, 10:26 PM
  3. classpath problem, out of ideas
    By edzet in forum Advanced Java
    Replies: 16
    Last Post: 05-14-2010, 12:38 PM
  4. Ideas anyone?
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-03-2009, 08:17 AM
  5. Need ideas here
    By Manfizy in forum New To Java
    Replies: 0
    Last Post: 07-30-2009, 08:07 AM

Tags for this Thread

Posting Permissions

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