(转) java排列组合算法(n选m)

来源:互联网 发布:python多进程共享对象 编辑:程序博客网 时间:2024/06/07 06:53

转载自http://blog.sina.com.cn/s/blog_822120bf010147mi.html


package test;

 
import java.util.ArrayList;
import java.util.List;
 
public class Test {
    private static char[] is = new char[] { '1', '2', '4', '5', '6', '7', '8', '9'};
    private static int total;
    private static int m = 6;
    public static void main(String[] args) {
        List<Integer> iL = new ArrayList<Integer>();
        new Test().plzh("", iL,  m);
        System.out.println("total : " + total);
    }
    private void plzh(String s, List<Integer> iL, int m) {
        if(m == 0) {
            System.out.println(s);
            total++;
            return;
        }
        List<Integer> iL2;
        for(int i = 0; i < is.length; i++) {
            iL2 = new ArrayList<Integer>();
            iL2.addAll(iL);
            if(!iL.contains(i)) {
                String str = s + is[i];
                iL2.add(i);
                plzh(str, iL2, m-1);
            }
        }
    }
}
0 0