0

在 n=5 和 k=3 的情况下,以下循环将执行此操作

List<String> l=new ArrayList<String>();
l.add("A");l.add("B");l.add("C");l.add("D");l.add("E");
int broadcastSize = (int) Math.pow(2, l.size());
for (int i = 1; i < broadcastSize; i++) {
    StringBuffer buffer = new StringBuffer(50);
    int mask = i;
    int j = 0;   
    int size=0;
    System.out.println();
    while (mask > 0) {
        if ((mask & 1) == 1) {
            System.out.println(".. "+mask);
            buffer.append(l.get(j));
            if (++size>3){
                buffer = new StringBuffer(50);
                break;
            }
        }
        System.out.println(" "+mask);
        mask >>= 1;
        j++;
    }
    if (buffer.length()>0)
        System.out.println(buffer.toString());

}

但效率不高我想用银行家的序列来做,因此先探索单例,然后是对,然后是三元组并停止。

我没有找到这样做的方法,但至少这个循环应该更有效:

List<String> l=new ArrayList<String>();
l.add("A");l.add("B");l.add("C");l.add("D");l.add("E");
int broadcastSize = (int) Math.pow(2, l.size());
for (int i = 1; i < broadcastSize; i++) {
    StringBuffer buffer = new StringBuffer(50);
    int mask = i;
    int j = 0;   

    if (StringUtils.countMatches(Integer.toBinaryString(i), "1") < 4){
        while (mask > 0) {
            if ((mask & 1) == 1) {
                buffer.append(l.get(j));

            }
            mask >>= 1;
            j++;
        }
        if (buffer.length()>0)
            System.out.println(buffer.toString());
    }


}

还有:但是k个嵌入式循环看起来很难看

//singleton
for (int i = 0; i < l.size(); i++) {
    System.out.println(l.get(i));
}

//pairs
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        System.out.println(l.get(i)+l.get(j));
    }
}

//3-tuple
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        for (int k = j+1; k < l.size(); k++) {
            System.out.println(l.get(i)+l.get(j)+l.get(k));
        }
    }
}
//...
// k-tuple
4

3 回答 3

4

这种技术被称为Gosper's hack。它仅适用于n <= 32因为它使用了 a 的位int,但如果使用 a ,您可以将其增加到 64 long

int nextCombo(int x) {
  // moves to the next combination with the same number of 1 bits
  int u = x & (-x);
  int v = u + x;
  return v + (((v ^ x) / u) >> 2);
}

...
for (int x = (1 << k) - 1; (x >>> n) == 0; x = nextCombo(x)) {
  System.out.println(Integer.toBinaryString(x));
}

对于n = 5k = 3,这打印

111
1011
1101
1110
10011
10101
10110
11001
11010
11100

完全符合您的预期。

于 2012-05-31T18:14:47.710 回答
2

这应该是最有效的方法,即使 k 个嵌入式循环看起来很丑

//singleton
for (int i = 0; i < l.size(); i++) {
    System.out.println(l.get(i));
}

//pairs
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        System.out.println(l.get(i)+l.get(j));
    }
}

//3-tuple
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        for (int k = j+1; k < l.size(); k++) {
            System.out.println(l.get(i)+l.get(j)+l.get(k));
        }
    }
}
// ...
//k-tuple
于 2012-05-31T18:10:34.757 回答
0

Apache commons 具有大小为 k的子集和permutations的迭代器。这是一个迭代器,它遍历一个 n 元组的 1-k 个元组,它结合了两者:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.iterators.PermutationIterator;
import org.apache.commons.math3.util.Combinations;

public class AllTuplesUpToKIterator implements Iterator<List<Integer>> {
    private Iterator<int[]> combinationIterator;
    private PermutationIterator<Integer> permutationIterator;
    int i;
    int k;
    int n;

    public AllTuplesUpToKIterator(int n, int k) {
        this.i = 1;
        this.k = k;
        this.n = n;
        combinationIterator = new Combinations(n, 1).iterator();
        permutationIterator = new PermutationIterator<Integer>(intArrayToIntegerList(combinationIterator.next()));
    }

    @Override
    public boolean hasNext() {
        if (permutationIterator.hasNext()) {
            return true;
        } else if (combinationIterator.hasNext()) {
            return true;
        } else if (i<k) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Integer> next() {
        if (!permutationIterator.hasNext()) {
            if (!combinationIterator.hasNext()) {
                i++;
                combinationIterator = new Combinations(n, i).iterator();
            }
            permutationIterator = new PermutationIterator<Integer>(intArrayToIntegerList(combinationIterator.next()));          
        }
        return permutationIterator.next();
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }

    public static List<Integer> intArrayToIntegerList(int[] arr) {
        List<Integer> result = new ArrayList<Integer>();
        for (int i=0; i< arr.length; i++) {
            result.add(arr[i]);
        }
        return result;
    }


    public static void main(String[] args) {
        int n = 4;
        int k = 2;
        for (AllTuplesUpToKIterator iter= new AllTuplesUpToKIterator(n, k); iter.hasNext();) {
            System.out.println(iter.next());
        }

    }
}
于 2015-05-18T07:24:28.180 回答