0

how can i change this for loop to recursive in the event that if groupSize=3, n=6 will print 123 124 125 126 134 135 136 145 146 156 234 235 236 245 246 345 346 356 456

public static void printCombinations(int groupSize, int n){

    if (groupSize <=n && groupSize>0 && n>0){

            for (int i=1; i<=n; i++){
                for (int j=1; j<=i-1; j++){
                    for (int k=1; k<=j-1; k++){
                        int first = k;
                        int second = j;
                        int third = i;
                        if (i!=j && i!=k && j!=k){
                            System.out.println(first +" " + second +" "+ third);
                            }
                    }
                }   
            }
        }
    }
8
  • 1. n can't be less than zero of groupSize is greater than 0 and less than n. 2. Your second for loop won't run because the first value of i is 1, so when j is 1, it's going to be greater than 1-1, which is 0. Commented Oct 1, 2011 at 20:49
  • Can you post what you have tried so far? Commented Oct 1, 2011 at 20:49
  • 3
    @Walkerneo: Seems like a reasonable use of recursion to me. Commented Oct 1, 2011 at 20:50
  • @MarkByers, yeah, I thought he was doing something else initially Commented Oct 1, 2011 at 20:51
  • OK, so you're trying to print all numbers with the amount of digits in groupSize using the numbers 1 through n such that the the digit in the hundred's place is less than the digit in the tens is less than the digit in the ones? Commented Oct 1, 2011 at 20:56

2 Answers 2

1

Probably going to be rough around the edges; I'm just learning Java.

class comboMaker {
    public static void main(String args[]){
        String[] test1 = startCombo(3,6);
        prnt(test1);
    }
    private static String[] startCombo(int len,int digits){
        return combos(len,digits,"",0);
    }
    private static String[] combos(int len,int digits,String cur,int lastdig){
        if (len>digits){
            return null;
        }
        if (cur.length()==len){
            return new String[]{cur};           
        }
        String tmp = cur;
        String[] rtn = {};
        for(int i = lastdig+1;i<=digits;i++){
            tmp=cur+Integer.toString(i);
            rtn=concat(rtn,combos(len,digits,tmp,i));   
        }
        return rtn;

    }
    private static String[] concat(String[] A, String[] B) {
           String[] C= new String[A.length+B.length];
           System.arraycopy(A, 0, C, 0, A.length);
           System.arraycopy(B, 0, C, A.length, B.length);
           return C;
    }

    private static void prnt(String[] str){
        if(str==null){
            System.out.println("Invalid string array");
            return;
        }
        for(int i =0;i<str.length;i++ ){
            System.out.println(str[i]);
        }   
    }   
}

I included a method to concatenate arrays that I found here: How can I concatenate two arrays in Java?

Also a method to print your string array generated with the combo maker.

Sign up to request clarification or add additional context in comments.

6 Comments

considering this was probably homework, it would have probably been better not to have given such a complete / explicit answer.
Maybe it's not homework. Maybe this will help him when he otherwise would have failed the class. I honestly have no clue, but I'm trying to learn Python and Java, so I just wanted the experience.
Thank everyone for the example given, I am in the process of learning java and this is about to learn recursion, the code above is written in not a good way though if I change the groupSize to 4, it will not work. So, I plant to write it in recursion way. =)
Output if using comboStart(4,6): 1234 1235 1236 1245 1246 1256 1345 1346 1356 1456 2345 2346 2356 2456 3456 The length of the numbers generated can't be more than the number of digits given, or else there aren't enough digits for each value to be greater than the last.
user963501: If you found this answer to be helpful, you should give @Walkerneo credit
|
0

Create a method that will produce the suffix to a particular value with x digits and return a list of all possible suffixes. Might be something like:

List<String> generateSuffixes(int prefix, int suffixSize, int maxDigitValue);

The method would iterate from prefix + 1 to maxDigitValue. It would then call itself with the iterate value and suffixSize - 1. It would append the iterate to each suffix generated to create the list to return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.