[Project Euler] Problem 18 & Problem 67 Python解答

用python来解决的,python就是干脆利索。代码少。python编码还是需要努力啊

import io



# read data

f = open("D:\Euler\E18\Test.txt", "r")

graph = {}

lineno = 0

while 1:

    data = f.readline()

    if not data:

        break

    else:

        linenodes = data.strip().split()

        for i in range(0, len(linenodes)):

            linenodes[i] = int(linenodes[i])

        graph[lineno] = linenodes

        lineno = lineno + 1

f.close()



# find the biggest path

path = {}

for i in range(0, lineno):

    path[i] = []

    if i == 0:

        path[0] = graph[i]    

    else:

        path[i].append(path[i-1][0] + graph[i][0])        

        for j in range(1, i):

            path[i].append(max(path[i-1][j-1], path[i-1][j]) + graph[i][j])

        path[i].append(path[i-1][i-1] + graph[i][i])



print max(path[lineno-1])

  

速度还可以,算67题也基本都是三秒以内。

你可能感兴趣的:(project)