java - 用公式 N^R 的所有组合填充数组

标签 java arrays combinations backtracking recursive-backtracking

对于家庭作业问题,我需要用公式N^R的所有组合填充一个数组。变量R 是常量,为6。变量 N 不是常量,假设它是 2。所以2^6 = 64。现在我需要的是一个包含所有组合的数组(在本例中为 64)。我找到了一个网站,它完全满足我的需要,在这种情况下的输出应该是:

[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0],
[0, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 0],
[1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 0, 0],
[1, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0],
[1, 1, 0, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 0, 1],
[1, 1, 0, 1, 1, 0],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1]

我尝试用 for 循环实现这一点,但没有成功。

我不想要使这成为可能的算法的完整代码,但是 我希望在路上得到帮助。提前致谢。

最佳答案

我想出了这个解决方案,它有点笨拙,但可能适合您的情况,注释应该解释一切:

public static void printCombinations(int R, int N) {
    // calculate the combinations
    String[][] combinations = calculateCombinations(R, N);
    // iterate over all
    for (int i = 0; i < combinations.length; i++) {
        // prints the commas at the end
        if (i != 0) {
            System.out.println(',');
        }
        // print to std out
        System.out.print(Arrays.toString(combinations[i]));
    }
    System.out.println();
}

public static String[][] calculateCombinations(int R, int N) {
    // calculate our limit
    int limit = (int) StrictMath.pow(N, R);
    // create the result array
    String[][] result = new String[limit][R];
    // iterate over all possibilities
    for (int i = 0; i < limit; i++) {
        // convert to base
        String base = Long.toString(i, N);
        // holds our temporary value
        StringBuilder intermediate = new StringBuilder(R);
        // pad the value from the start with zeroes if needed
        for (int sub = R - base.length(); sub > 0; sub--) {
            intermediate.append('0');
        }
        // append our number
        intermediate.append(base);

        // append to result
        result[i] = intermediate.toString().split("");
    }
    // return the result
    return result;
}

然后可以像这样调用以漂亮地打印出来:

printCombinations(6, 2);

或者得到结果:

String[][] result = calculateCombinations(6, 2);

Running Demo

关于java - 用公式 N^R 的所有组合填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55846886/

相关文章:

python - 想要将列表的每个元素与 n 个列表的每个元素组合

java - 在 Fragment 中使用 Activity 方法

java - 等待函数有逻辑错误

c++ - 有没有办法使用初始化为第一行的指针访问二维数组的第二行?

arrays - 两个嵌套表集合之间的plsql差异

c++ - 自 C++20 以来,是否允许对分配的存储进行指针运算?

list - 使用较长列表中的任意两个元素创建列表 DrRacket

java - 反射。执行带参数的非静态方法

java - Linkedin 集成 Android

MySQL 修复了来自多个表的允许组合