哪位高手有高效的组合算法啊,给我一个,谢谢大家了,不要递归的算法。
可以计算100中取50个元素的所有组合,而且可以全部输出。在此谢谢大家了!

解决方案 »

  1.   

    C 50 100 = A 50 100/ A 50 50(or 50!)A:排列
    C:组合
    m,n:n个元素中取m个元素 A m n = n*(n-1)*...(n-m+1)C m n = A m n/A m m = n*(n-1)*...(n-m+1)/m! 
      

  2.   

    递归本来是高效的,逻辑简单写
    难道你想写50个for循环?
    O(∩_∩)O哈哈~
      

  3.   


    ///可取重复
    ///amd cpu 2800+,1.61GHz
    ///取三位141ms,四位14s,再多不敢试了    public static void main(String[] args) {
            short[] dd = new short[100];
            for (short i = 0; i < 100; i++) {
                dd[i] = i;
            }
            long t = System.currentTimeMillis();
            printf(dd, 4);
            System.out.flush();
            System.out.println(System.currentTimeMillis() - t);
        }    /**看成是keys.length进制的数,打印select位数字
        */
        static void printf(byte[] keys, int select) {
            byte[] num = new byte[select];
            int length = keys.length;
            int pos;
            for (int i = 0; i < select; i++) {//给num赋初值
                num[i] = keys[0];
            }
            int nowpos = select - 1;
            for (long i = 1, j, imax = (long) Math.pow(keys.length, select); i < imax; i++) {
                if (i % length == 0) {//有进位则更新多位
                    j = i;
                    nowpos = select - 1;
                    while (j % length == 0) {
                        num[nowpos--] = keys[0];
                        j /= length;
                    }
                    num[nowpos] = (byte) j;
                } else {//无进位则更新个位 ++
                    num[select - 1] = keys[(int) i % length];
                }
    //            System.err.print(i + ":[");
    //            for (pos = 0; pos < select; pos++) {//打印值
    //                System.err.print(num[pos] + (pos < select - 1 ? "," : ""));
    //            }
    //            System.err.println("]");
            }
        }///
    ///数据结构,这个算不算public class NewClass1 {    public static void main(String[] args) {
            short[] dd = new short[100];
            for (short i = 0; i < 100; i++) {
                dd[i] = i;
            }
            Coll c = new Coll(dd, 50);        ///从1111开始1000个打印
            for (int i = 0; i < 10; i++) {
                short[] num = c.get(1111 + i);
                int select = num.length;
                System.err.print(":[");
                for (int pos = 0; pos < select; pos++) {//打印值
                    System.err.print(num[pos] + (pos < select - 1 ? "," : ""));
                }
                System.err.println("]");
            }
        }
    }
    /**
    */
    class Coll {    private short[] keys;
        private int select;    public Coll(short[] keys, int select) {
            if (keys == null || keys.length < 1 || select < 1 || select >= keys.length) {
                throw new java.lang.ArrayIndexOutOfBoundsException("" + select);
            }
            this.keys = keys;
            this.select = select;
    //        Arrays.sort(this.keys);
        }    /**得到总长度*/
        public long getSize() {
            return (long) Math.pow(keys.length, select);
        }    /**取第index个组合*/
        public short[] get(long index) {
            if (index < 0 || index >= getSize()) {
                throw new java.lang.ArrayIndexOutOfBoundsException("" + index);
            }
            short[] re = new short[select];
            for (int i = select - 1; i > -1; i--) {
                if (index > keys.length) {
                    re[i] = keys[(int) (index % keys.length)];
                    index /= keys.length;
                } else if (index > -1) {
                    re[i] = keys[(int) (index)];
                    index = -1;
                } else {
                    re[i] = keys[0];
                }
            }
            return re;
        }    /**取全部组合*/
        public List<short[]> getList() {
            List list = new java.util.LinkedList<short[]>();
            short[] num = new short[select];
            int length = keys.length;
            int pos;
            for (int i = 0; i < select; i++) {//给num赋初值
                num[i] = keys[0];
            }
            int nowpos = select - 1;
            for (long i = 1, j, imax = getSize(); i < imax; i++) {
                if (i % length == 0) {//有进位
                    j = i;
                    nowpos = select - 1;
                    while (j % length == 0) {
                        num[nowpos--] = keys[0];
                        j /= length;
                    }
                    num[nowpos] = (byte) j;
                } else {//无进位则个位 ++
                    num[select - 1] = keys[(int) i % length];
                }
                short[] buf = new short[select];
                System.arraycopy(num, 0, buf, 0, length);
                list.add(buf);
            }
            return list;
        }
    }