求组合算法-----m个元素中选n个

组合算法 
本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 
代表的数被选中,为0则没选中。 
首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 
然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为 
“01”组合,同时将其左边的所有“1”全部移动到数组的最左端。 
当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得 
到了最后一个组合。 

abcde

例如求5中选3的组合: 
1 1 1 0 0 //1,2,3 abc 
1 1 0 1 0 //1,2,4 abd
1 0 1 1 0 //1,3,4 acd
0 1 1 1 0 //2,3,4 bcd
1 1 0 0 1 //1,2,5 abe
1 0 1 0 1 //1,3,5 
0 1 1 0 1 //2,3,5 
1 0 0 1 1 //1,4,5 
0 1 0 1 1 //2,4,5 
0 0 1 1 1 //3,4,5  

一种代码实现。

public class Choose {
    /*
     * Choose m elements from a array which totally has n elements.
     * Algorithm Description
     * For example, choose 3 elements from 5 elements [a,b,c,d,e]
     * we need an number array that contains only 0 or 1 numbers
     * 1 represents choosed.
     * start: 11100
     * generate next by : 
     * scan from left to right, find the first 10, switch it to 01, scan to right, adjust the right part of the 
     * string to 11..00.. format( 1 is always at the left of 0).
     * 11(10)0 --> 1(10)10 --> (10)110 ---> 011(10) ---> 
     * 01(10)1 --> 0(10)11 --> 00111 
     * end
     */
    public void choose(char cArr[], int n){
        int m=cArr.length;
        int num[]=new int[m];
        for(int i=0;i<n;i++){
            num[i]=1;
        }
        for(int i=n;i<m;i++){
            num[i]=0;
        }
        int cnt=1;
        while(hasNext(num)){
            print(cArr,num);
            moveNext(num);
            cnt++;
        }
        print(cArr,num);
        System.out.println("Total Num:"+cnt);
    }
    public void print(char cArr[], int num[]){
        int i;
        for(i=0;i<num.length;i++){
            if(num[i]==1){
                System.out.print(cArr[i]);
            }
        }
        System.out.println();
    }
    private void moveNext(int num[]){
        int i,j;
        for(i=0;i<num.length-1;i++){
            if(num[i]==1&&num[i+1]==0){
                break;
            }
        }
        if(i==num.length-1){
            System.out.println("no Next");
            return;
        }
        num[i]=0;
        num[i+1]=1;
        int oneCnt=0;
        for(j=0;j<i;j++){
            if(num[j]==1){
                oneCnt++;
            }
        }
        for(j=0;j<i;j++){
            if(j<oneCnt){
                num[j]=1;
            } else {
                num[j]=0;
            }
        }
    }
    private boolean hasNext(int num[]){
        int i,j;
        for(i=0;i<num.length;i++){
            if(num[i]==0){
                continue;
            } else {
                break;
            }
        }
        for(j=i;j<num.length;j++){
            if(num[j]==1){
                continue;
            } else {
                break;
            }
        }
        return (j<num.length);
    }
    public static void main(String args[]){
        Choose c=new Choose();
        char cArr[]=new char[]{'a','b','c','d','e','f','g','h'};
        c.choose(cArr, 3);
    }
}
posted @ 2012-07-28 18:07  xxx's blog  阅读(649)  评论(3编辑  收藏  举报