我对Euler项目的方法,p84缺少什么?

时间:2012-06-23 18:34:33

标签: python algorithm

我正在通过欧拉项目解决问题。我已经达到question 84并且我已经创建了游戏模拟。我已根据问题中提供的统计数据运行模拟,并得到了正确的序列。当我尝试用2d4运行时,我得到了错误的结果。通常我会得到101516.我错过了什么?

请注意,我不是在寻找解决方案,也不是要修复我的代码。我只想知道我的算法存在缺陷。

from random import randint
import sys

pos = 0
doubles = 0

csqs = [2,17,33] #The cc position
hsqs = [7,22,36] #The ch positions
rounds = 0
stop = 100000
sqs = dict() #will store how many visit I had on each square


for i in range (0,40): #initial values of sqs
    sqs[i] = 0

def doCC():# the cc cards. There is no real effect of randomly picking a card or kipping them in order on my result
    global pos,doubles
    global CC
    if CC == 0: 
         pos = 0
    elif CC == 1: 
         pos = 10
         doubles = 0
    CC += 1
    if CC == 16: CC = 0

def doCH(): #CH cards
    global pos, doubles, CH
    if CH == 0: pos = 0
    elif CH == 1: 
        pos = 10
        doubles = 0
    elif CH == 2: pos = 11
    elif CH == 3: pos = 24
    elif CH == 4: pos = 39
    elif CH == 5: pos = 5
    elif CH == 6 or CH == 7: 
        if pos == 7:
            pos = 15
        elif pos == 22:
            pos = 25
        elif pos == 36:
            pos = 5
    elif CH ==8:
            if pos == 22: pos = 28
            else: pos = 12
    elif CH == 9:
            pos -= 3
    CH += 1
    if CH == 16: CH = 0

while rounds < stop:
    d1 = randint(1,4)
    d2 = randint(1,4)
    if d1 == d2: 
        doubles += 1 #counting doubles
    else:
        doubles = 0
    if doubles == 3:
        pos = 10
        doubles = 0
    pos += d1+d2
    if pos>= 40 : #you have just crossed go
        pos -= 40
        rounds += 1
        if rounds %10000 == 0: print rounds,
    if pos == 30: #g2j
        doubles = 0
        pos = 10
    if pos in hsqs: 
        doCH()
    if pos in csqs: 
        doCC()
    sqs[pos] += 1
    sys.stdout.flush()
# Setting values
m1 = 0
m2 = 0
m3 = 0
v1 = 0
v2 = 0
v3 = 0


su = 0
for v in sqs:
    m = sqs[v]
    su += m
    if m > m1:
        m1 = m
        v1 = v

for v in sqs:
    m = sqs[v]
    if m > m2 and m < m1:
        m2 = m
        v2 = v

for v in sqs:
    m = sqs[v]
    if m > m3 and m < m2:
        m3 = m
        v3 = v

for v in sqs:
    sqs[v] = sqs[v]*100.0/su

print m1,m2,m3
print v1,v2,v3
print m1*100.0/su, m2*100.0/su, m3*100.0/su

print sqs

1 个答案:

答案 0 :(得分:1)

当你连续三个双打时,

if doubles == 3:
    pos = 10
    doubles = 0
pos += d1+d2

你仍然跳出监狱,但你不应该。