一些假设:
我想我会按顺序尝试所有可能的组合,看看哪一个加起来是21,但是有太多的方法来混合卡片(52!方法)。这种方法也没有考虑到顺序不重要,也没有考虑到任何一张牌最多只能有4种类型(黑桃、梅花、钻石、红桃)。在
现在我想的问题是:
我们有11个“插槽”。每一个插槽里都有53个可能的东西:52张卡中的1张或者根本没有卡。之所以设11个槽,是因为11张牌是最多可以发的牌数,加起来还是21张;11张以上的牌加起来要超过21张。在
然后“最左边”的插槽将递增一个,并检查所有11个插槽,看它们是否加起来为21(0表示插槽中没有卡)。否则,右边的下一个插槽将递增,下一个插槽,依此类推。在
一旦前4个插槽包含相同的“卡”(在四个增量之后,前4个插槽都将是1),第五个插槽就不可能是这个数字,因为有4个任何类型的数字。然后,第五个插槽将成为剩余可用卡中的下一个最低编号;在四个1的情况下,第五个插槽将变成2,以此类推。在
你会怎么做?在
在担心花色和不同的价值
10
的牌之前,让我们先弄清楚有多少不同的价值组合导致21
。例如,5, 5, 10, 1
就是这样一种组合。以下函数接受limit
这是目标值,start
表示可以拾取的最低值,used
是拾取值的列表:由于我们关注的是不同的卡片组合而不是不同的价值组合,因此应该修改上面的基本情况,以返回可用于生成价值组合的不同卡片组合的数量。幸运的是,这是一个非常简单的任务,Binomial coefficient可以用来计算从
n
项中选择多少个不同的k
项组合。在一旦知道}到{},值{}到{}。对于所有其他值
^{2}$used
中每个值的不同卡片组合的数量,它们就可以相互相乘以获得最终结果。例如5, 5, 10, 1
值5
结果为bcoef(4, 2) == 6
,值{bcoef(x, 0)
结果为1
。将这些值相乘得到6 * 16 * 4 == 384
,然后返回:分而治之,利用这样的知识,如果你有13和选择10,你只需选择的卡和3左看。。。警告这个解决方案可能很慢(在我的盒子上花了大约180秒。。。这绝对不是最佳的。。在
按以下步骤设置您的卡
^{2}$那就打电话给你的部门
不幸的是,这确实允许一些复制品潜入。。。 为了得到唯一的条目,你需要稍微改变一下
在
sum_to
的最后一行将其更改为然后当你得到你的组合,你必须做一些深黑色伏都教风格的Python
从这个很酷的问题中,我学到了以下几点(记住,在真实的游戏中,你会让庄家和其他玩家也从同一个牌堆中移出)
记住,这些通常是次优的打法(即考虑A,10->1,10和击球)
所以我决定写一个脚本来检查每一个可能的手。总数是188052。因为我检查了所有可能的组合,所以这是确切的数字(与估计值相反):
在输出中,第一列是抽签中的牌数,第二列是累积数。因此,输出中“3”后面的数字是2张牌抽签和3张牌抽签的21手的总数。小写字母a表示低ace(1分),大写字母a表示高位ace。我有一个行(使用set命令的那一行),以确保它抛出任何有重复卡的手。在
脚本运行需要36分钟。所以在执行时间和准确性之间肯定有一个权衡。“大名单”包含解决方案(即每只手的总和为21)
^{2}$相关问题 更多 >
编程相关推荐