Java:写出包含K 1的所有N位序列

时间:2015-02-21 12:03:13

标签: java arrays algorithm

我应该编写一个程序,打印出所有可能的N位序列,其中K 1s ,其余的(N - K)将为0。还应该有一个计数器,指示最后有多少序列。

在我的情况下,N = 9且K = 3,所以程序应该写出这样的东西:

111000000
110100000
...
101100000
101010000
...
000000111
Total: 84

到目前为止,我的代码看起来像这样

// N bits and K 1s
import java.util.ArrayList;
import java.util.Arrays;

public class Sequence {

    public static void main(String[] args) {

        ArrayList<int[]> all = new ArrayList<>();
        int counter = 0;
        int first = 0;
        int second;
        int third;

        for (int i = first; i < 9; i++) {
            int[] sequence = {0, 0, 0, 0, 0, 0, 0, 0, 0};
            // the 1st "1"
            sequence[i] = 1;

            second = i + 1;

            for (int j = second; j < 9; j++) {
                int[] seq2 = sequence;
                // the 2nd "1"
                seq2[j] = 1;

                third = j + 1;
                for (int l = third; l < 9; l++) {
                    int[] seq3 = seq2;
                    // the 3rd "1"
                    seq3[l] = 1;
                    all.add(seq3); 
                    counter++;

                    seq3[l] = 0;
                    third++;
                }

                second++;
            }   

            first++;
        }

        for (int[] sequences : all) {
            System.out.println(Arrays.toString(sequences));
        }
        System.out.println("Total: " + counter);
    }   
}

但它似乎没有这样做,我无法弄清楚为什么。代码在java中,我使用9个整数的数组的ArrayList作为9位序列。

2 个答案:

答案 0 :(得分:1)

你几乎做对了。

你的问题在这里:

int[] seq2 = sequence;

在这里:

int[] seq3 = seq2;

这些分配意味着您只有一个数组可以不断更改并添加到输出列表中。

将它们更改为:

int[] seq2 = Arrays.copyOf(sequence, sequence.length);

和:

int[] seq3 = Arrays.copyOf(seq2, seq2.length);

同样删除此行(因为在每次迭代中创建了一个新数组,因此无需清除l元素:

seq3[l] = 0;

你会得到这个输出:

[1, 1, 1, 0, 0, 0, 0, 0, 0]
[1, 1, 0, 1, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 1, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 1, 0, 0, 0]
[1, 1, 0, 0, 0, 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, 1, 0]
[0, 0, 0, 0, 1, 0, 1, 0, 1]
[0, 0, 0, 0, 1, 0, 0, 1, 1]
[0, 0, 0, 0, 0, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 1, 1, 0, 1]
[0, 0, 0, 0, 0, 1, 0, 1, 1]
[0, 0, 0, 0, 0, 0, 1, 1, 1]
Total: 84

答案 1 :(得分:1)

一种简单的方法是使用基于字符串的方法。这是一个完整的工作解决方案:

int k = 3, n = 9, count = 0;
for (int i = 1 << n; i < 2 << n; i++) {
    if (Long.toString(i, 2).replace("0", "").length() == k + 1) {
        System.out.println(Long.toString(i, 2).substring(1));
        count++;
    }
}
System.out.println("Total: " +count);

我测试了这段代码,它产生了正确的输出。这会在一个数字范围内进行迭代,并使用额外的前导数据来整齐地回避截断的前导零问题。

请注意,此实现仅处理n最多62个。对于大于62的n,将循环类型更改为BigInteger

int k = 3, n = 9, count = 0;
BigInteger end = new BigInteger("2").pow(n + 1);
for (BigInteger i = new BigInteger("2").pow(n); i.compareTo(end) < 0; i = i.add(BigInteger.ONE)) {
    if (i.toString( 2).replace("0", "").length() == k + 1) {
        System.out.println(i.toString( 2).substring(1));
        count++;
    }
}
System.out.println(count);

此实现适用于n

的任意大值

如果你看一下这段代码并想一想“oooh,performance!?”,这个代码在第一版上大约需要20ms,在我的普通硬件上大约需要40ms - 足够快。


顺便说一句,如果您必须拥有输出顺序,请将循环更改为倒计时而不是向上计数。