打印出从列表的 Y 元素中选择的 X 个不同对象的所有组合
例如,一手 52 张牌的扑克牌中有 5 张牌 = 2598960 种组合。
我实际上将如何显示所有这些组合?
查找数字的代码很简单:
def binomial_coef(total,subset)
factorial(total) / (factorial(subset) * factorial(total - subset))
end
def factorial(n)
n.downto(1).inject(:*)
end
# different 5 card poker hand combinations
cards = 52
hand_number = 5
puts binomial_coef(cards, hand_number)
关于打印所有实际组合的解决方案有什么想法吗?
例如:
1,2,3,4,5
1,2,3,4,6
等等
,甚至帮助入门。 谢谢!
For instance, 5 cards in a poker hand of 52 cards = 2598960 combinations.
How would I actually display all those combinations though?
The code to find the number is easy:
def binomial_coef(total,subset)
factorial(total) / (factorial(subset) * factorial(total - subset))
end
def factorial(n)
n.downto(1).inject(:*)
end
# different 5 card poker hand combinations
cards = 52
hand_number = 5
puts binomial_coef(cards, hand_number)
Any ideas on a solution to printing out the all the actual combinations?
eg:
1,2,3,4,5
1,2,3,4,6
etc.
Or even help gettings started.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
Array#combination
You need
Array#combination