数组成员数组java的组合(骰子滚?)

时间:2015-10-04 15:42:30

标签: java arrays combinations dice

真的很抱歉没有写我在哪里,因为我不知道如何开始并且没有运气搜索。

例如,如果我有

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面)我希望通过抛出所有可能的骰子对得到所有可能的组合(dice1 + dice2与dice2 + dice1相同,所以我不喜欢#39; t想要那个)。我不知道我将拥有多少骰子以及我将拥有多少方。

任何指针,开始建议或任何事情都非常感谢,谢谢

1 个答案:

答案 0 :(得分:0)

首先,你可以做这样的事情。只是一个解决方案,时间复杂度不是那么大。

    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]);
                }
            }
        }
    }
相关问题