枚举每五张牌的组合

发布于 2024-08-23 13:39:14 字数 397 浏览 6 评论 0原文

我刚刚遇到了一个脑块,我有一个 Deck 对象,并且想以迭代方式从中获取每 5 张卡牌组合。有人可以告诉我如何做到这一点,我想它会是:

for(int i =0; i <52; i++){
    for(int j = i + 1 ; j < 52; j++){
        for(int k = j + 1; k < 52; k++{ 
            for(int l = k + 1; l < 52; l++){
                for(int m = l + 1; m < 52; m++){
                }
             }
         }
     }
  }

这是正确的吗?

谢谢

I have just had a brain block, I have a Deck object and want to get every 5 card combination from it in a iterative manner. Could someone show me how to do this, I would imagine it would be:

for(int i =0; i <52; i++){
    for(int j = i + 1 ; j < 52; j++){
        for(int k = j + 1; k < 52; k++{ 
            for(int l = k + 1; l < 52; l++){
                for(int m = l + 1; m < 52; m++){
                }
             }
         }
     }
  }

Is this correct?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-08-30 13:39:14

是的,效果很好。如果你想枚举所有n张卡的组合,这是行不通的。

为此,您需要递归。将0张牌放入槽0中。递归枚举剩余n-1个槽中的所有n-1手牌(不包括0)。重复,将卡 1 插入插槽 0。非常简单。

编辑:一些代码:

private static final int NUM_CARDS = 52;

public static void main(String[] args) {
  enumerateAllHands(Integer.parseInt(args[0]));
}

private static void enumerateAllHands(int n) {
  if (n > NUM_CARDS) {
    throw new IllegalArgumentException();
  }
  int[] cards = new int[n];
  BitSet cardsUsed = new BitSet();
  enumerate(cards, 0, cardsUsed);
}

private static void enumerate(int[] cards, int from, BitSet cardsUsed) {
  if (from == cards.length) {
    emit(cards);
  } else {
    for (int i = 0; i < NUM_CARDS; i++) {
      if (!cardsUsed.get(i)) {
        cards[from] = i;
        cardsUsed.set(i);
        enumerate(cards, from + 1, cardsUsed);
        cardsUsed.clear(i);
      }
    }
  }
}

private static void emit(int[] cards) {
  System.out.println(Arrays.toString(cards));
}

Yes, that works fine. If you want to enumerate all n-card combinations, this doesn't work.

For that, you'd need recursion. Put card 0 in slot 0. Recursively enumerate all n-1 card hands (excluding 0) in the remaining n-1 slots. Repeat, with card 1 in slot 0. Pretty easy.

EDIT: some code:

private static final int NUM_CARDS = 52;

public static void main(String[] args) {
  enumerateAllHands(Integer.parseInt(args[0]));
}

private static void enumerateAllHands(int n) {
  if (n > NUM_CARDS) {
    throw new IllegalArgumentException();
  }
  int[] cards = new int[n];
  BitSet cardsUsed = new BitSet();
  enumerate(cards, 0, cardsUsed);
}

private static void enumerate(int[] cards, int from, BitSet cardsUsed) {
  if (from == cards.length) {
    emit(cards);
  } else {
    for (int i = 0; i < NUM_CARDS; i++) {
      if (!cardsUsed.get(i)) {
        cards[from] = i;
        cardsUsed.set(i);
        enumerate(cards, from + 1, cardsUsed);
        cardsUsed.clear(i);
      }
    }
  }
}

private static void emit(int[] cards) {
  System.out.println(Arrays.toString(cards));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文