我试图找到21张5卡纸牌手,如果有5张卡纸牌和2张孔牌,它们可以组成。这是我到目前为止的内容:

public String[] joinArrays(String[] first, String[] second) {
        String[] result = new String[first.length + second.length];
        for (int i = 0; i < first.length; i++) {
            result[i] = first[i];
        }
        for (int i = 0; i < second.length; i++) {
            result[i + first.length] = second[i];
        }
        return result;
    }

    public String[][] getAllPossibleHands(String[] board, String[] hand){
        String[] allCards = joinArrays(board, hand);
        String[][] allHands = new String[21][5];
        ...
    }


关于从这里去哪里的任何提示?我有一个由7个元素组成的数组,并希望从这些元素创建可成形的7C5(21)5个元素数组。

谢谢!

最佳答案

对于每只手,请选择7张不使用的卡中的两张。加所有其他

int cardsSelected = 0;
int hand = 0;
// select first card not to be in the hand
for(int firstCard = 0; firstCard < 7; firstCard++){
    // select first card not to be in the hand
    for(int secondCard = firstCard + 1; secondCard < 7; secondCard++){
        // every card that is not the first or second will added to the hand
        for(int i = 0; i < 7; i++){
            if(i != firstCard && i != secondCard){
                allHands[hand][cardsSelected++] = allCards[i];
            }
        }
        // next hand
        cardsSelected = 0;
        hand++;
    }
}

关于java - 给定7张牌,获得所有可能的5张牌扑克手,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33859993/

10-11 02:32