欧拉计划(python) problem 67

Maximum path sum II

Problem 67

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3

7 4

4 6

8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom in triangle.txt (right click and ‘Save Link/Target As...‘), a 15K text
file containing a triangle with one-hundred rows.

NOTE: This is a much more difficult version of Problem 18. It is not
possible to try every route to solve this problem, as there are 299 altogether! If you could check one trillion (1012) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve
it. ;o)


Answer:
7273
Completed on Fri, 30 Jan 2015, 14:21

Go to the thread for problem 67 in the forum.

解题思路:动态规划

python code :

import math

sqrt=math.sqrt

a=[]

f = open("data.txt")

line = f.readline()

while line:

row=line.split(‘ ‘)

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

a.append(int(row[i]))

line = f.readline()

f.close()

k=len(a)

def getsun(x):

redult=[];

k=int((sqrt(1+8*x)-1)/2)+1

return [x+k,x+k+1]

def getrowstart(x):

return int(x*(x-1)/2)

for i in range(99,0,-1):

start=getrowstart(i)

for j in range(0,i):

sun=getsun(start+j)

a[start+j]+=max(a[sun[0]],a[sun[1]])

print(a[0])

time: <1s

时间: 2024-11-08 14:58:30

欧拉计划(python) problem 67的相关文章

欧拉计划(python) problem 18

Maximum path sum I Problem 18 By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top t

欧拉计划(python) problem 1

problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. python code: k3=0; k5=0; result=0; for i in range(1,10

欧拉计划(python) problem 5

Smallest multiple Problem 5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? python code : imp

欧拉计划(python) problem 7

10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? python code : import math sqrt=math.sqrt def func(x): k=int(sqrt(x))+1 for i in range(2,k)

欧拉计划(python) problem 8

Largest product in a series Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 858

欧拉计划(python) problem 26

Reciprocal cycles Problem 26 A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: 1/2 =  0.5 1/3 =  0.(3) 1/4 =  0.25 1/5 =  0.2 1/6 =  0.1(6) 1/7 =  0.(142857) 1/8 =  0.12

欧拉计划(python) problem 27

Quadratic primes Problem 27 Euler discovered the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible

欧拉计划(python) problem 29

Distinct powers Problem 29 Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 If they are then placed in numerical orde

欧拉计划(python) problem 22

Names scores Problem 22 Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multipl