简体   繁体   English

欧拉计划#18:最大路径总和I

[英]Project Euler #18: Maximum Path Sum I

So basically I interpreted this problem as follows: 所以基本上我将这个问题解释为:

   3
  7 4
 2 4 6
8 5 9 3

The two number below the starting should be compared and the larger should be picked as the new number. 应该比较开头以下的两个数字,并选择较大的作为新数字。 So in this case, it would be 3, then 7, then 4, then 9. Sum them up, get the answer of 23. I wrote a program to achieve this: 因此,在这种情况下,它将是3,然后是7,然后是4,然后是9。将它们加起来,得到23的答案。我编写了一个程序来实现此目的:

public class ProblemEighteen {

private static int pos = 1;

public static void main(String[] args) {
    try {
        Scanner in = new Scanner(new File("Problem18Text"));
        int sum = 0;
        while (in.hasNextLine()) {
            final String line = in.nextLine();
            int big = getBiggestNum(line);
            sum += big;
            System.out.println(big);
        }
        System.out.println(sum);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static int getBiggestNum(String line) {
    final String[] numbers = line.split(" ");
    if (numbers.length == 1) {
        pos = 1;
        return Integer.parseInt(numbers[0]);
    } else {
        int i = 1;
        int numOne = -1;
        int numTwo = -1;
        for (final String num : numbers) {
            if (pos == i) {
                numOne = Integer.parseInt(num);
            } else if (pos + 1 == i) {
                numTwo = Integer.parseInt(num);
            }
            i++;
            if (numOne != -1 && numTwo != -1)
                break;
        }
        if (numOne > numTwo) {
            return numOne;
        } else {
            pos += 1;
            return numTwo;
        }
    }
}

It works fine for the example I gave above, but when I put in the actual problem to solve, it said I got it wrong (I got 1064). 对于我上面给出的示例,它工作正常,但是当我输入实际问题来解决时,它说我错了(我得到1064)。 I added a print statement to see what numbers it was selecting and it got them all right (based on how I understood what I was trying to find) but I still got it wrong... Anyone know why? 我添加了一个打印语句,以查看它选择了哪些数字,并使其完全正确(基于我对所要查找内容的理解),但我仍然理解错了……有人知道为什么吗?

It's been a while since I've solved this problem, but you'll find that it's best that you start at the bottom of the triangle and work your way up. 自从我解决了这个问题已经有一段时间了,但是您会发现最好从三角形的底部开始并逐步向上。 Especially, when you get to problem 67. 特别是当您遇到问题67时。

If you read your data into an array as baum suggested, you'd have data that looks like this: 如果按照鲍姆(Baum)建议将数据读入数组,则数据看起来像这样:

3  0  0  0
7  4  0  0
2  4  6  0
8  5  9  3

So start at the bottom taking two numbers at a time and comparing a sum to their adjacent number in the next above row. 因此,从底部开始,一次取两个数字,然后将总和与下一行的相邻数字进行比较。

8 + 2 = 10 or 5 + 2 = 7.  10 is greater so replace the 2 with the 10.  
5 + 4 = 9 or 9 + 4 = 13.  13 is greater so replace the 4 with the 13.
9 + 6 = 15 or 3 + 6 = 9.  15 is greater so replace the 6 with 15.

Now move up one row and perform the same checks until you get to the very top and the very top should contain the correct answer. 现在,向上移动一行并执行相同的检查,直到到达最顶部并且最顶部应该包含正确的答案。

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

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