2 olk0612 olk0612 于 2014.02.18 19:39 提问

!求助:python 列表读取时的问题

project euler problem 81题
我写了下面的代码,origin里面是5x5的矩阵,运行结果正常
但是我把origin换成一个80x80的矩阵的时候,在读取383568的时候却读取成了326735
我没有找出代码的问题,但是在codepad和terminal上运行也是错误答案
求助大牛啊!!大恩不言谢

origin = ['131,673,234,103,18', '201,96,342,965,150', '630,803,746,422,111', '537,699,497,121,956', '805,732,524,37,331']
matrix = []

for row in origin:
i = origin.index(row)
to_add = []
lst = row.split(',')

if i == 0:
    for string in lst:
        j = lst.index(string)
        number = int(string)
        if j != 0:
            #问题出在下面一行,前面的循环都正确,但是循环到第一行倒数第三项时,to_add最后一项明明是383568,但是 print to_add[j - 1] 显示为326735  
                number += to_add[j - 1]
        to_add.append(number)

else:
    '''for string in lst:
        j = lst.index(string)
        number = int(string)
        if j == 0:
            number += matrix[i - 1][j]
        else:
            number += min(matrix[i - 1][j], to_add[j - 1])
        to_add.append(number)'''
        break
matrix.append(to_add)

print matrix

另附上80x80矩阵第一行 origin = ['4445,2697,5115,718,2209,2212,654,4348,3079,6821,7668,3276,8874,4190,3785,2752,9473,7817,9137,496,7338,3434,7152,4355,4552,7917,7827,2460,2350,691,3514,5880,3145,7633,7199,3783,5066,7487,3285,1084,8985,760,872,8609,8051,1134,9536,5750,9716,9371,7619,5617,275,9721,2997,2698,1887,8825,6372,3014,2113,7122,7050,6775,5948,2758,1219,3539,348,7989,2735,9862,1263,8089,6401,9462,3168,2758,3748,5870']

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!