我编写了下面的程序来迭代每一只可能的扑克手,并计算出其中有多少只手是一双。
一只手就是任何五张牌。
单对牌是指同一等级的两张牌(数)和其他三张不同等级的牌,例如(1、2、1、3、4)。
我把这副牌代表成一个数字列表。
然而,它找到的单双手数= 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)发布于 2015-07-13 11:43:49
问题是你的手也可以包含以下类型的卡片-
一双三只,一双一双
你实际上也在把这算成一对。
我修改了代码,只计算出手的数量,这样它就包含了一个类型的三只手,以及一对手。密码-
deck = []
for i in range(52):
deck.append((i//13 + 1, i%13 + 1))
def pairCount(hand):
paircount = 0
threecount = 0
for i in hand:
count = 0
for x in hand:
if x[1] == i[1]:
count += 1
if count == 2:
paircount += .5 #Adding 0.5 because each pair is counted twice
if count == 3:
threecount += 0.33333333
return (round(paircount, 0) , round(threecount, 0))
count = 0
for i in combinations(deck, 5):
if pairCount(i) == (1.0, 1.0):
count += 1这个数字计算为- 3744.
现在,如果我们从你得到的数字- 1101984 -我们得到你期望的数字- 1098240中减去这个数字。
https://stackoverflow.com/questions/31381901
复制相似问题