2015-11-22 110 views
1

我試圖找到21張5張撲克牌,可以給予5張牌和2張牌。這是我到目前爲止:獲得所有可能的5張牌紙牌給予7張牌

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元素數組。

謝謝!

回答

2

對於每一隻手,選擇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++; 
    } 
} 
相關問題