如何按递增顺序计算组合?

时间:2017-02-26 11:32:22

标签: java combinatorics

说我有3个数组或有序集:a [0,3] b [1,4] c [2,5]我想计算以下结果:[0,1,2],[0,1 ,5],[0,1,2,5],[0,1,4,5],[0,4,5],[3,4,5],[0,3,4,5]没有动力装置,由不断增加的秩序组合而成。我无法想出一个逻辑。请帮忙。我想要的只是所有可能组合的计数。[0,3,4]不会被算作有效结果,因为我想要的是每个结果应该至少有一个来自数组a,b,c的元素

        int ai = 0; int aj = A.size()-1;
        while(aj >= ai) {
            int curA = A.get(aj);
            int indexA = Collections.binarySearch(B, curA);
            List<Integer> subB;
            if(indexA >= 0) {
                subB = B.subList(indexA, B.size());
            } else {
                 int insertionPointA = -(indexA+1);
                 subB = B.subList(insertionPointA, B.size());
            }

            int bi = 0; int bj = subB.size()-1;
            while(bj >= bi) {
                int curB = subB.get(bj);
                int indexB = Collections.binarySearch(C, curB);
                List<Integer> subC;
                if(indexB >= 0) {
                    subC = C.subList(indexB, C.size());
                } else {
                    int insertionPointB = -(indexB+1);
                    subC = C.subList(insertionPointB, C.size());
                }

                count += Math.pow(2, subC.size()) - 1;    


                bj--;
            }

            aj--;
        }

以上代码遗漏了[0,4,5]和[3,4,5]

for(int a : A) {
                int indexB = Collections.binarySearch(B, a);
                List<Integer> subB;
                if(indexB >= 0) {
                    subB = B.subList(indexB, B.size());
                } else {
                     int insertionPointB = -(indexB+1);
                     subB = B.subList(insertionPointB, B.size());
                }
                for(int b : subB) {
                    int indexC = Collections.binarySearch(C, b);
                    List<Integer> subC;
                    if(indexC >= 0) {
                        subC = C.subList(indexC, C.size());
                    } else {
                         int insertionPointC = -(indexC+1);
                         subC = C.subList(insertionPointC, C.size());
                    }
                    count += Math.pow(2, subC.size()) - 1;  
                }
            }

上面的代码错过了[0,3,4,5]和[0,1,4,5]

0 个答案:

没有答案