打印出从列表的 Y 元素中选择的 X 个不同对象的所有组合

发布于 2024-10-01 06:00:14 字数 506 浏览 0 评论 0原文

例如,一手 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 技术交流群。

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

发布评论

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

评论(3

素衣风尘叹 2024-10-08 06:00:14

您需要 Array#combination

cards = (1..52).to_a
hand_number = 5
cards.combination(hand_number).to_a

=> [[1,2,3,4,5],[1,2,3,4,6],...]

You need Array#combination

cards = (1..52).to_a
hand_number = 5
cards.combination(hand_number).to_a

=> [[1,2,3,4,5],[1,2,3,4,6],...]
眼眸里的快感 2024-10-08 06:00:14
(1..52).to_a.combination(5)
(1..52).to_a.combination(5)
橘和柠 2024-10-08 06:00:14
puts (1..52).to_a.combination(5).to_a.inspect
puts (1..52).to_a.combination(5).to_a.inspect
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文