简体   繁体   English

错误 output 找到金字塔/三角形中相邻数字的最大和

[英]Wrong output in finding the maximum sum of adjacent digits in a pyramid/triangle

I solving Problem 18 on Project Euler and have written the code for it as below:在 Project Euler 上解决了问题 18,并为它编写了如下代码:

v = '''75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23'''.strip().split('\n')


last_ind = 0
max_sum = 75
for row in v[1:]:
    row = row.split(' ')
    num1 = int(row[last_ind])
    num2 = int(row[last_ind+1])
    if num1 > num2:
        max_sum+=num1
    else:
        max_sum+=num2
        last_ind = last_ind+1
        
print(max_sum)    

I got the ANSWER AS 1064 but it's written 1074 everywhere.我得到的答案是1064 ,但到处都写着1074 Can someone please suggest me what I might be doing wrong.有人可以建议我我可能做错了什么。 By calculating every row by hands, I get 1064 .通过手工计算每一行,我得到1064 What is wrong here?这里有什么问题?

You are assuming that the optimal path will always move down via the child with the greatest value, but this is not true.您假设最佳路径将始终通过具有最大值的孩子向下移动,但事实并非如此。 A child with a lesser value may open up a possibility (at lower layers) to find a much greater value, which more than compensates for the temporary less optimal value.具有较小值的子节点可能会打开(在较低层)找到更大值的可能性,这足以弥补暂时不太理想的值。

So your algorithm, in its first iteration will go from 75 to the 95 on the second row.因此,您的算法在第一次迭代中将 go 从 75 到第二行的 95。 But this turns out to be the wrong choice.但事实证明这是错误的选择。 You'll have to come up with a better algorithm.你必须想出一个更好的算法。 You will find inspiration in other Q&A about this particular challenge, like this one .您将在其他关于这一特定挑战的问答中找到灵感,例如这个

Here you see the optimal path:在这里您可以看到最佳路径:

path小路
75 75
95 64 95 64
17 47 82 17 47 82
18 35 87 10 18 35 87 10
20 04 82 47 65 20 04 82 47 65
19 01 23 75 03 34 19 01 23 75 03 34
88 02 77 73 07 63 67 88 02 77 73 07 63 67
99 65 04 28 06 16 70 92 99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33 41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29 41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14 53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57 70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48 91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31 63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

Imagine you were on the row just before the bottom row, call it row l .想象一下,你在底行之前的那一行,称之为 row l Now for each number m on row l , ask (1) which of the (one or) two choices would be optimal to choose next?现在对于第l行上的每个数字m ,问(1)(一个或)两个选择中的哪一个是下一个最佳选择? (2) what would the sum now look like at m if we added that optimal choice? (2) 如果我们加上最优选择,现在在m处的总和会是什么样子?

Now ask the same questions for the row just above l .现在对l上方的行提出相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM