Java 二维阵列中的元素组合数量是否可能?
我有5x5元素表。假设表中的每一行都是一个罐子。行中的每个元素都是罐子中不同颜色的球。 我们从第一个罐子拿一个球,从第二个罐子拿一个球,从第三个罐子拿一个球。。。依此类推到第五罐。 我们有5个球的颜色组合。。。然后我们把球放回相关的罐子里。 问题:可能有多少种组合变体? 回答n^n,其中n是表格大小 问题是,我永远不知道表有多大,尽管它总是对称的(nxn)元素。我想写一个通用的方法,它将返回所有可能的颜色组合 对于表5x5元素,它如下所示:Java 二维阵列中的元素组合数量是否可能?,java,arrays,Java,Arrays,我有5x5元素表。假设表中的每一行都是一个罐子。行中的每个元素都是罐子中不同颜色的球。 我们从第一个罐子拿一个球,从第二个罐子拿一个球,从第三个罐子拿一个球。。。依此类推到第五罐。 我们有5个球的颜色组合。。。然后我们把球放回相关的罐子里。 问题:可能有多少种组合变体? 回答n^n,其中n是表格大小 问题是,我永远不知道表有多大,尽管它总是对称的(nxn)元素。我想写一个通用的方法,它将返回所有可能的颜色组合 对于表5x5元素,它如下所示: private int combinations =
private int combinations = 0;
private char table[][] = { {'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','X','Y','Z'}};
public Prog() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int k= 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
for (int m = 0; m < 5; m++) {
System.out.println(table[0][i] +" " + table[1][j]+ " " + table[2][k]+ " " + table[3][l]+ " " + table[4][m]);
combinations++;
}
System.out.println("--------------");
}
}
}
}
System.out.println("Total combination is : " + combinations);
}
私有整数组合=0;
私有字符表[][]={{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','X','Y','Z'};
公共程序(){
对于(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
对于(int k=0;k<5;k++){
对于(int l=0;l<5;l++){
对于(int m=0;m<5;m++){
System.out.println(表[0][i]+“”+表[1][j]+“”+表[2][k]+“”+表[3][l]+“”+表[4][m]);
组合++;
}
System.out.println(“--------------”;
}
}
}
}
System.out.println(“总组合为:“+组合”);
}
。。。但上述代码仅适用于5x5表。如果我得到4x4或3x3,我需要修改所有for循环以正常工作。。。
有人能帮我写一个方法,根据表的大小修改自己并返回正确的组合吗
谢谢 使用数组的。
length
字段确定表的大小:
for (int i = 0; i < table.length; i++) {
(..)
for(int i=0;i
此字段始终包含正确的数组大小。使用数组的。
长度
字段确定表的大小:
for (int i = 0; i < table.length; i++) {
(..)
for(int i=0;i
此字段始终包含正确大小的数组。此问题的递归解决方案:
import java.math.BigInteger;
import java.util.Arrays;
/**
* Created for http://stackoverflow.com/q/22892808/1266906
*/
public class Combinations {
public static BigInteger printCombinationsRecursively(char[][] table) {
return printCombinationsRecursively(table, new char[table.length], 0);
}
public static BigInteger printCombinationsRecursively(char[][] table, char[] selection, int currentRow) {
if(currentRow >= table.length) {
System.out.println(Arrays.toString(selection));
return BigInteger.ONE;
}
BigInteger count = BigInteger.ZERO;
for (char c : table[currentRow]) {
selection[currentRow] = c;
count = count.add(printCombinationsRecursively(table, selection, currentRow + 1));
}
return count;
}
public static void main(String[] args) {
char[][] table = new char[][] {
new char[] {'A', 'B', 'C', 'D'},
new char[] {'E', 'F', 'G', 'H'},
new char[] {'I', 'J', 'K', 'L'},
new char[] {'M', 'N', 'O', 'P'}
};
final BigInteger combinations = printCombinationsRecursively(table);
System.out.println(combinations + " combinations");
}
}
此问题的递归解决方案:
import java.math.BigInteger;
import java.util.Arrays;
/**
* Created for http://stackoverflow.com/q/22892808/1266906
*/
public class Combinations {
public static BigInteger printCombinationsRecursively(char[][] table) {
return printCombinationsRecursively(table, new char[table.length], 0);
}
public static BigInteger printCombinationsRecursively(char[][] table, char[] selection, int currentRow) {
if(currentRow >= table.length) {
System.out.println(Arrays.toString(selection));
return BigInteger.ONE;
}
BigInteger count = BigInteger.ZERO;
for (char c : table[currentRow]) {
selection[currentRow] = c;
count = count.add(printCombinationsRecursively(table, selection, currentRow + 1));
}
return count;
}
public static void main(String[] args) {
char[][] table = new char[][] {
new char[] {'A', 'B', 'C', 'D'},
new char[] {'E', 'F', 'G', 'H'},
new char[] {'I', 'J', 'K', 'L'},
new char[] {'M', 'N', 'O', 'P'}
};
final BigInteger combinations = printCombinationsRecursively(table);
System.out.println(combinations + " combinations");
}
}
5x5阵列具有相同输出的迭代版本:
void Prog() {
int baseN = table.length;
int maxDigits = table[0].length;
int max = (int) Math.pow(baseN, maxDigits);
// each iteration of this loop is another unique permutation
for (int i = 0; i < max; i++) {
int[] digits = new int[maxDigits];
int value = i;
int place = digits.length - 1;
while (value > 0) {
int thisdigit = value % baseN;
value /= baseN;
digits[place--] = thisdigit;
}
int tableIdx = 0;
for (int digitIdx = 0; digitIdx < digits.length; digitIdx++) {
int digit = digits[digitIdx];
System.out.print(table[tableIdx][digit] + " ");
tableIdx++;
}
System.out.println();
combinations++;
if (i % maxDigits == maxDigits - 1)
System.out.println("--------------");
}
System.out.println("Total combination is : " + combinations);
}
void Prog(){
int baseN=表格长度;
int maxDigits=表[0]。长度;
int max=(int)Math.pow(baseN,maxDigits);
//该循环的每次迭代都是另一个唯一的置换
对于(int i=0;i0){
int thisdigit=值%baseN;
值/=baseN;
数字[位置--]=此数字;
}
int tableIdx=0;
对于(int digitIdx=0;digitIdx
这是基于我的答案,我把它当作一个5位数,以5为基数的数字
注意:因为我对
max
使用int
,而你对组合使用它,所以这个限制是9x9数组,因为10^10>整数。max_值长将给你15x15,但这将需要年运行!!!迭代版本,5x5数组的输出相同:
void Prog() {
int baseN = table.length;
int maxDigits = table[0].length;
int max = (int) Math.pow(baseN, maxDigits);
// each iteration of this loop is another unique permutation
for (int i = 0; i < max; i++) {
int[] digits = new int[maxDigits];
int value = i;
int place = digits.length - 1;
while (value > 0) {
int thisdigit = value % baseN;
value /= baseN;
digits[place--] = thisdigit;
}
int tableIdx = 0;
for (int digitIdx = 0; digitIdx < digits.length; digitIdx++) {
int digit = digits[digitIdx];
System.out.print(table[tableIdx][digit] + " ");
tableIdx++;
}
System.out.println();
combinations++;
if (i % maxDigits == maxDigits - 1)
System.out.println("--------------");
}
System.out.println("Total combination is : " + combinations);
}
void Prog(){
int baseN=表格长度;
int maxDigits=表[0]。长度;
int max=(int)Math.pow(baseN,maxDigits);
//该循环的每次迭代都是另一个唯一的置换
对于(int i=0;i0){
int thisdigit=值%baseN;
值/=baseN;
数字[位置--]=此数字;
}
int tableIdx=0;
对于(int digitIdx=0;digitIdx
这是基于我的答案,我把它当作一个5位数,以5为基数的数字
请注意,因为我使用int
表示max
,而您使用它表示组合
,所以此数组的限制是9x9数组,因为10^10>整数。max_值
长
将为您提供高达15x15的数据,但这将需要年才能运行!!!我认为这些方法不是很理想,这里是general是一种从NxN表中获取排列的方法。这是Javascript中的,但给出了该方法的思想
var table = [ ['A','B','C','D','E'],
['F','G','H','I','J'],
['K','L','M','N','O'],
['P','Q','R','S','T'],
['U','V','X','Y','Z']];
function perm(l) {
var n = Math.pow(l.length,l.length);
for(var i=0; i < n; i++) {
var s = '';
var m = i;
for(var k=0 ; k < l.length; k++) {
var p = m % 5;
s += l[k][p];
m = ~~(m / 5);
}
console.log(s);
}
}
perm(table);
var表=[[A'、'B'、'C'、'D'、'E'],
[F'、'G'、'H'、'I'、'J'],
[K'、'L'、'M'、'N'、'O'],
[P'、'Q'、'R'、'S'、'T'],
[U'、'V'、'X'、'Y'、'Z'];
函数perm(l){
var n=数学功率(l.长度,l.长度);
对于(变量i=0;i
我认为这些方法不是很理想,这里是烫发的一般方法