我写了下面的程序,迭代每一个可能的扑克手,并计算有多少手是一对
一手牌是任意5张牌。
单对是指两张牌的等级(编号)相同,其他三张牌的等级不同,例如(1,2,1,3,4)
我将一副牌表示为数字列表,例如
-1=ACE
-2=两个
-3=三
…
-11=插孔
-12=大号。。。在
这个程序看起来很管用, 它找到的单对手的数量=1101984
但根据多方消息来源,正确答案是1098240。在
有人能看到我代码中的错误在哪里吗?在
from itertools import combinations
# Generating the deck
deck = []
for i in range(52):
deck.append(i%13 + 1)
def pairCount(hand):
paircount = 0
for i in hand:
count = 0
for x in hand:
if x == i:
count += 1
if count == 2:
paircount += .5 #Adding 0.5 because each pair is counted twice
return paircount
count = 0
for i in combinations(deck, 5): # loop through all combinations of 5
if pairCount(i) == 1:
count += 1
print(count)
问题是你的手牌也可以包含以下类型的牌-
实际上,你也把它算作一对。在
我修改了代码,只计算手的数量,这样它就包含了三个一类的以及一对一对的。代码-
这将数字计数为-
3744
。在现在,如果我们从你得到的数字中减去这个数字-
1101984
-我们就得到了你想要的数字-1098240
。在相关问题 更多 >
编程相关推荐