Results 1 to 2 of 2
  1. #1
    Claymz is offline Member
    Join Date
    Apr 2011
    Posts
    25
    Rep Power
    0

    Question N while loops into recursion

    Hi all,

    so, i have this list of elements, and what i want to do, is firstly read a number from console,which tells the program how many elements must be excluded from the list, and then print all possible combinations of this modified element list (ElementList.lenght - n). As an example, if i have a list, that contains 7 elements, and user types into the console 2, that means that the program needs to print out all possible combinations of elements, which can be made out of any 5 elements contained in that same list.

    I think that it is possible to make it with nested while loops (i wasn't able though), but only for a fixed user input number, so thats why i need to use recursion.

    Ty in advance!

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

    Default

    Quote Originally Posted by Claymz View Post
    Hi all,

    so, i have this list of elements, and what i want to do, is firstly read a number from console,which tells the program how many elements must be excluded from the list, and then print all possible combinations of this modified element list (ElementList.lenght - n). As an example, if i have a list, that contains 7 elements, and user types into the console 2, that means that the program needs to print out all possible combinations of elements, which can be made out of any 5 elements contained in that same list.

    I think that it is possible to make it with nested while loops (i wasn't able though), but only for a fixed user input number, so thats why i need to use recursion.

    Ty in advance!
    You don't need recursion: suppose you have n elements and you want to find all combinations m <= n. e.g. n == 3 and m == 2, so have the element 0, 1 and 2 and you want to list the combinations 01, 02, 12. Given a current combination you can find the next one with this method:

    Java Code:
    boolean next(int[] comb, int n) {
    	for (int i= comb.length; --i >= 0;) {
    		if (++comb[i] <= n-(comb.length-i)) {
    			for (; ++i < comb.length; )
    				comb[i]= comb[i-1]+1;
    			return true;
    		}
    	}
    	return false;
    }
    The method returns true if a next combination could be found and false otherwise. Simply apply this method in a loop while it returns true.

    kind regards,

    Jos
    Last edited by JosAH; 04-17-2011 at 03:08 PM. Reason: darn, my copy and paste failed ... corrected now
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. Recursion
    By fam2315 in forum New To Java
    Replies: 7
    Last Post: 03-22-2011, 12:23 AM
  3. Recursion?
    By Fingerz in forum New To Java
    Replies: 10
    Last Post: 01-08-2011, 02:25 AM
  4. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  5. help with recursion
    By Nari in forum New To Java
    Replies: 15
    Last Post: 04-24-2008, 09:13 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
  •