欧拉项目#8错误的答案,需要解释

时间:2019-04-25 09:22:49

标签: python

免责声明:-我不需要解决方案。我只想知道为什么我的代码无法产生正确的答案。

我为Euler项目问题​​8编写了以下代码,该代码基本上要求在1000个数字块中找到具有最大乘积的13个相邻数字。 这是代码:-

PropertyFactory.iconAllowOverlap(true)

答案,我得到-283606606020024000
谁能解释我要去哪里错了?

2 个答案:

答案 0 :(得分:2)

我相信您应该在每次循环后重置total

end = 0
nxt = 13
total = 1
temp = 0
while nxt < 1000: 
    for i in que[end:nxt]:
        total *= int(i)
        if total > temp:
            temp = total

    end += 1
    nxt += 1
    total = 1

答案 1 :(得分:2)

    nxt = 13

    while nxt < 1000: 
        for i in que[end:nxt]:
                    total *= int(i)
        if total > temp:
            temp = total
        total = 1
        end += 1
        nxt += 1