简体   繁体   English

欧拉计划-67-我不知道为什么我的代码给出了错误的答案

[英]Project euler - 67 - I don't know why my code gives a wrong answer

the problem is this https://projecteuler.net/problem=67 问题是这个https://projecteuler.net/problem=67

I can´t understand why this gives a wrong answer- 6580, when it should be 7273. 我不明白为什么这会给出错误的答案-6580,而应该是7273。

the idea is to look at the adjacent numbers in the next line and see wich is higher and sum that one. 我们的想法是查看下一行中的相邻数字,然后查看更高的数字并将其相加。 So I'm comparing the number in position j with the one in position j+1. 所以我正在比较位置j的数字和位置j + 1的数字。 If they were equal then it would go in the next line and compare the next 3 numbers, but it doesn't run into 2 equal numbers so I removed the code that dealt with that. 如果它们相等,则它将进入下一行并比较接下来的3个数字,但是它不会遇到2个相等的数字,因此我删除了处理该数字的代码。

data is a list where in each position has a list with each line of the triangle 数据是一个列表,其中每个位置都有一个三角形的每一行的列表

data = open('triangulo.txt', 'r')
data = data.readlines()
j = 0

soma = int(data[0].strip())

for i in range(1,100):
    if int(data[i].strip().split()[j])==int(data[i].strip().split([j+1]):
        print('fodfsf') #this is just to see if there are any numbers equal to each other
    if int(data[i].strip().split()[j])>int(data[i].strip().split()[j+1]):
        soma = soma + int(data[i].strip().split()[j])
    else:
        soma = soma + int(data[i].strip().split()[j+1])
        j = j + 1


print(soma)

Consider the following triangle. 考虑下面的三角形。

         1
      1     2
   9     1     1
9     1     1     1

For this triangle, what would your algorithm think is the maximal path? 对于这个三角形,您的算法会认为最大路径是什么?

You are going to need to find a different approach to solving this, but that's half the fun of Project Euler problems. 您将需要找到一种不同的方法来解决此问题,但这仅是Euler项目难题的一半。

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

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