1

例如,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

甚至帮助入门。谢谢!

4

3 回答 3

6

你需要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],...]
于 2010-11-08T03:44:38.470 回答
1
puts (1..52).to_a.combination(5).to_a.inspect
于 2010-11-08T03:54:29.050 回答
1
(1..52).to_a.combination(5)
于 2010-11-08T03:45:46.390 回答