枚举每五张牌的组合
我刚刚遇到了一个脑块,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,效果很好。如果你想枚举所有n张卡的组合,这是行不通的。
为此,您需要递归。将0张牌放入槽0中。递归枚举剩余n-1个槽中的所有n-1手牌(不包括0)。重复,将卡 1 插入插槽 0。非常简单。
编辑:一些代码:
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: