简体   繁体   English

从标准卡座子集中获得每两张卡的组合

[英]get every two card combination from subset of standard deck

I have a standard deck of cards and then have removed a few, from the remaining cards I want to calculate all of the possible two card combinations. 我有一张标准的纸牌,然后从剩余的纸牌中删除了几张,我想计算所有可能的两种纸牌组合。 For example with 47 cards there is 47 choose 2 combinations. 例如,对于47张卡片,有47种选择2种组合。 Can anyone think of an efficient way to do this other than 除此以外,谁能想到一种有效的方法

foreach(card){
  combinations.add(card, card +1)
}

Thanks 谢谢

for(int i=0; i<47; i++) {
  for(int j=i+1; j<47; j++) {
     combinations.add(i, j);
  }
}

This is the most efficient way, as it goes through each pair only once. 这是最有效的方法,因为它仅对每对进行一次。

If you just want the number of two-pair combinations, see here . 如果仅需要两对组合的数量 ,请参见此处

On this site there are some algorithms solving combinatoric problems. 在该站点上 ,有一些算法可以解决组合问题。 Look for a class called 寻找一个叫做

class ChoiceIterable<T> implements Iterable<T[]>

If all you need is just two card subsets (as opposed to a variable number), you could easily do this with two nested for loops. 如果您只需要两个卡子集(而不是可变数量),则可以使用两个嵌套的for循环轻松地做到这一点。

for(i=0;i<cards.length;i++){
   for(j=i+1;j<cards.length;j++){
      combinations.add(cards[i],cards[j]);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM