数组成员数组的组合 java(掷骰子?)
combinations of an array of array members java ( dice roll? )
真的很抱歉没有写下我现在的位置,因为我不知道如何开始,也没有找到好运。
例如,如果我有
int [][]={{1,2,3}, //possible states of 1 member
{10,20,30}, //possible states of 2 member
{100,200,300}} //possible states of 3 member
我需要定义 k - 组合成员数组的数量,然后得到结果,我得到它们状态的所有可能组合。所以基本上如果 k 是 2:
member(1st array)1 - member2
member1 - member3
member2 - member3
然后获取这些成员所有可能状态的组合,即成员不能相互组合。
你可以想象有 3 个骰子(在这种情况下都是 3 面的),我想通过抛出所有可能的骰子对来获得所有可能的组合(骰子 1 + 骰子 2 与骰子 2 + 骰子 1 相同,所以我不想要那个)。我不知道我会有多少个骰子,我会有多少面。
非常感谢任何指点、入门建议或任何东西,谢谢
首先,您可以这样做。只是一个解决方案,时间复杂度没那么高。
for(int i=0; i < x.length; i++){ // Selects one dice
for(int j = i + 1; j < x.length; j++){ // Select another dice
for(int k=0; k<x[i].length; k++){ // selects face of first dice
for(int l=0; l<x[j].length; l++){ // select face of 2nd dice
System.out.print("Dice " + i + " Face " + k + " vs Dice " + j + " Face " + l);
System.out.println(" ==> " + x[i][k]+ " - " + x[j][l]);
}
}
}
}
真的很抱歉没有写下我现在的位置,因为我不知道如何开始,也没有找到好运。
例如,如果我有
int [][]={{1,2,3}, //possible states of 1 member
{10,20,30}, //possible states of 2 member
{100,200,300}} //possible states of 3 member
我需要定义 k - 组合成员数组的数量,然后得到结果,我得到它们状态的所有可能组合。所以基本上如果 k 是 2:
member(1st array)1 - member2
member1 - member3
member2 - member3
然后获取这些成员所有可能状态的组合,即成员不能相互组合。
你可以想象有 3 个骰子(在这种情况下都是 3 面的),我想通过抛出所有可能的骰子对来获得所有可能的组合(骰子 1 + 骰子 2 与骰子 2 + 骰子 1 相同,所以我不想要那个)。我不知道我会有多少个骰子,我会有多少面。
非常感谢任何指点、入门建议或任何东西,谢谢
首先,您可以这样做。只是一个解决方案,时间复杂度没那么高。
for(int i=0; i < x.length; i++){ // Selects one dice
for(int j = i + 1; j < x.length; j++){ // Select another dice
for(int k=0; k<x[i].length; k++){ // selects face of first dice
for(int l=0; l<x[j].length; l++){ // select face of 2nd dice
System.out.print("Dice " + i + " Face " + k + " vs Dice " + j + " Face " + l);
System.out.println(" ==> " + x[i][k]+ " - " + x[j][l]);
}
}
}
}