简体   繁体   中英

Find all combinations 6 number JAVA

I have a problem with combinations.

I need increment last number from 6 number and if the last number is 50 penultimate increment by 1.

I have this code which find all permutations. But I want combinations.

I need so if already program generate 1 2 3 4 5 6 never again generate combination this number so 1 2 3 4 5 6 it's the same like 6 5 4 3 2 1 and 6 1 2 3 4 5 ...

hladaj[N-2]++;
    while(((hladaj[N-3]==hladaj[N-2])||(hladaj[N-2]==hladaj[N-4])||(hladaj[N-2]==hladaj[N-5])||(hladaj[N-2]==hladaj[N-6])||(hladaj[N-2]==hladaj[N-7]))){
        hladaj[N-2]++;
    }

    if (hladaj[N-2]>MAXC) {
        hladaj[N-2]=hladaj[N-2]-MAXC;           
        hladaj[N-3]++;

        while(((hladaj[N-3]==hladaj[N-2])||(hladaj[N-2]==hladaj[N-4])||(hladaj[N-2]==hladaj[N-5])||(hladaj[N-2]==hladaj[N-6])||(hladaj[N-2]==hladaj[N-7]))){
            hladaj[N-2]++;
        }
        while(((hladaj[N-3]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-4])||(hladaj[N-3]==hladaj[N-5])||(hladaj[N-3]==hladaj[N-6])||(hladaj[N-3]==hladaj[N-7]))){
            hladaj[N-3]++;
        }

        if (hladaj[N-3]>MAXC) {
            hladaj[N-3]=hladaj[N-3]-MAXC;                   
            hladaj[N-4]++;
            while(((hladaj[N-3]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-4])||(hladaj[N-3]==hladaj[N-5])||(hladaj[N-3]==hladaj[N-6])||(hladaj[N-3]==hladaj[N-7]))){
                hladaj[N-3]++;
            }

            while(((hladaj[N-4]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-4])||(hladaj[N-4]==hladaj[N-5])||(hladaj[N-4]==hladaj[N-6])||(hladaj[N-4]==hladaj[N-7]))){
                hladaj[N-4]++;
            }           


            if (hladaj[N-4]>MAXC) {
                hladaj[N-4]=hladaj[N-4]-MAXC;
                hladaj[N-5]++;

                while(((hladaj[N-4]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-4])||(hladaj[N-4]==hladaj[N-5])||(hladaj[N-4]==hladaj[N-6])||(hladaj[N-4]==hladaj[N-7]))){
                    hladaj[N-4]++;
                }   

                while(((hladaj[N-5]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-5])||(hladaj[N-4]==hladaj[N-5])||(hladaj[N-5]==hladaj[N-6])||(hladaj[N-5]==hladaj[N-7]))){
                    hladaj[N-5]++;
                }


                if (hladaj[N-5]>MAXC) {
                    hladaj[N-5]=hladaj[N-5]-MAXC;
                    hladaj[N-6]++;

                    while(((hladaj[N-5]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-5])||(hladaj[N-4]==hladaj[N-5])||(hladaj[N-5]==hladaj[N-6])||(hladaj[N-5]==hladaj[N-7]))){
                        hladaj[N-5]++;
                    }

                    while(((hladaj[N-6]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-6])||(hladaj[N-4]==hladaj[N-6])||(hladaj[N-5]==hladaj[N-6])||(hladaj[N-6]==hladaj[N-7]))){
                        hladaj[N-6]++;
                    }

                    if (hladaj[N-6]>MAXC) {
                        hladaj[N-6]=hladaj[N-6]-MAXC;
                        hladaj[N-7]++;

                        while(((hladaj[N-6]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-6])||(hladaj[N-4]==hladaj[N-6])||(hladaj[N-5]==hladaj[N-6])||(hladaj[N-6]==hladaj[N-7]))){
                            hladaj[N-6]++;
                        }


                        while(((hladaj[N-7]==hladaj[N-2])||(hladaj[N-3]==hladaj[N-7])||(hladaj[N-4]==hladaj[N-7])||(hladaj[N-5]==hladaj[N-7])||(hladaj[N-6]==hladaj[N-7]))){
                            hladaj[N-7]++;
                        }

                        if (hladaj[N-7]>MAXC) {
                            hladaj[N-7]=MAXC;
                            return true;                                    
                        }                                   
                    }
                }                   
            }               
        }

This code generate 1 2 3 4 5 6 , 6 5 4 3 2 1 I need If generate one of combinations this combinations no generate again.

Please you have some ideas for modify my code or...

(I need all of generated number[6] processed, but now I waste time because I work 120 times with same numbers.)

Thanks

public static void main(String[] args){
    int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49};
    combinations2(arr, 7, 0, new int[7]);
}

static void combinations2(int[] arr, int pocet, int startPosition, int[] result){
    if (pocet == 0){
        System.out.println(Arrays.toString(result));
        return;
    }       
    for (int i = startPosition; i <= arr.length-pocet; i++){
        result[result.length - pocet] = arr[i];
        combinations2(arr, pocet-1, i+1, result);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM