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

对于一个家庭作业问题,我需要用公式的所有组合填充一个数组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 循环实现这一点,但没有成功。


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


守着星空守着你
浏览 71回答 1
1回答

慕尼黑8549860

我想出了这个解决方案,这有点笨拙,但可能适合你的情况,评论应该解释一切:public static void printCombinations(int R, int N) {&nbsp; &nbsp; // calculate the combinations&nbsp; &nbsp; String[][] combinations = calculateCombinations(R, N);&nbsp; &nbsp; // iterate over all&nbsp; &nbsp; for (int i = 0; i < combinations.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // prints the commas at the end&nbsp; &nbsp; &nbsp; &nbsp; if (i != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(',');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // print to std out&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(Arrays.toString(combinations[i]));&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}public static String[][] calculateCombinations(int R, int N) {&nbsp; &nbsp; // calculate our limit&nbsp; &nbsp; int limit = (int) StrictMath.pow(N, R);&nbsp; &nbsp; // create the result array&nbsp; &nbsp; String[][] result = new String[limit][R];&nbsp; &nbsp; // iterate over all possibilities&nbsp; &nbsp; for (int i = 0; i < limit; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // convert to base&nbsp; &nbsp; &nbsp; &nbsp; String base = Long.toString(i, N);&nbsp; &nbsp; &nbsp; &nbsp; // holds our temporary value&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder intermediate = new StringBuilder(R);&nbsp; &nbsp; &nbsp; &nbsp; // pad the value from the start with zeroes if needed&nbsp; &nbsp; &nbsp; &nbsp; for (int sub = R - base.length(); sub > 0; sub--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intermediate.append('0');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // append our number&nbsp; &nbsp; &nbsp; &nbsp; intermediate.append(base);&nbsp; &nbsp; &nbsp; &nbsp; // append to result&nbsp; &nbsp; &nbsp; &nbsp; result[i] = intermediate.toString().split("");&nbsp; &nbsp; }&nbsp; &nbsp; // return the result&nbsp; &nbsp; return result;}然后可以这样调用以漂亮地打印出来:printCombinations(6, 2);或者得到它的结果:String[][] result = calculateCombinations(6, 2);运行演示
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java