我正在编写一个小型纸牌游戏,其中两名玩家手中各有 4 张牌,他们必须按总和尽可能多地拿走 table 上的牌。它使用经典的扑克牌,所以相同的种子和相同的值。
King can only take another King
Queen can only take another Queen
Jack can only take another Jack
数字卡片可以按总和计算,例如:
10H takes 6C + 4S
5S takes 3C + 2D or 3S + 2D
7S takes 4S + 3S or 5C + 2D
And so on...
种子是不相关的……只有值是相关的。但问题是我需要计算独特的组合。因此,例如,我只想要其中一个组合,因为种子和值是相同的:
10H -> 6C + 4S
10H -> 4S + 6C
是否有任何预制函数可以执行此操作?我试着用谷歌和维基百科四处寻找,但可能我不知道算法/问题的英文名称。哦...我忘记了...解决方案可以是您想要的任何东西(普通、递归、linq)。
最佳答案
您尝试计算的组合称为 integer partitions .相应地,你在寻找 integer partition algorithm .
Python 中的解决方案可能如下所示:
def bubble(part, i=0): #add one to the list whilst keeping lexicographic order
if i == (len(part)-1) or part[i] < part[i+1]:
part[i] += 1
return part
else:
return bubble(part, i+1)
def partitions(n):
if n == 1:
yield [1]
return
for p in partitions(n-1):
yield [1] + p
yield bubble(p)
这在概念上类似于 Knuth 的 algorithm P in fascicle 3B .
关于c# - 独特组合的纸牌游戏算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14457575/