欧拉工程第67题:Maximum path sum II

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 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)

题目链接

这个题目只要把18题的稍微改下就好了

Java代码:

package project61;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class P67{

    void run() throws FileNotFoundException{
        String fileName="src\\project61\\p067_triangle.txt";
        Scanner input = new Scanner(new File(fileName));
        int[][] path = new int[100][100];
        for(int i = 0; i<100;i++){
            int j = 0;
            while(i>=j &&input.hasNext()){
                path[i][j] = input.nextInt();
                j++;
            }
        }
        for(int i= 99;i>=0;i--){
            for(int j= 0;j<i;j++){
                path[i-1][j] += Math.max(path[i][j], path[i][j+1]);
            }
        }
        System.out.println("result:"+path[0][0]);
    }
    public static void main(String[] args) throws FileNotFoundException{
        long start = System.currentTimeMillis();
        new P67().run();
        long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println("running time="+time/1000+"s"+time%1000+"ms");
    }
}

Python程序:

import urllib2
import time as time

def run():
    file_url = ‘https://projecteuler.net/project/resources/p067_triangle.txt‘
    table = [[int(n) for n in s.split()] for s in urllib2.urlopen(file_url).readlines()]

    for row in range(len(table)-1, 0, -1):
        for col in range(0, row):
            table[row-1][col] += max(table[row][col], table[row][col+1])
    print "Maximum top-bottom total in triangle:", table[0][0]

if __name__ == ‘__main__‘:
    start = time.time()
    run()
    print "running time:",(time.time() - start)
时间: 2024-08-03 11:13:15

欧拉工程第67题:Maximum path sum II的相关文章

[lintcode] Binary Tree Maximum Path Sum II

Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree and contain at least one node in it. 给一棵二叉树,找出从根节点出发的路径中,和最大的一条. 这条路径可以在任何二叉树中的节点结束,但是必须包含至少一个点(也就是根了). /** * Definition of TreeNode: * public class Tr

欧拉工程第70题:Totient permutation

题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个数 存在这样的数:N的欧拉数φ(n),是N的一个排列 例如:φ(87109)=79180 求在1---10^7中n/φ(n) 取到最小的 n 是多少? 这里的是p是n的素因子,当素因子有相同的时候只取一个 任意一个正整数都能分解成若干个素数乘积的形式 直接利用上题的phi函数就可以求解 这个是跑的最

欧拉工程第66题:Diophantine equation

题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小解. 问题转化为求根号n的连分数问题,分子就是x,分母就是y 要求的分子,分母,问题又转化为:根号n的连分数表示,对,求出其连分数表示就OK了 先求出a的序列是什么? 第64题,就是求a的序列的. a求出来了,要求出分子分母的表达式. 第65题,就是已经知道了a的序列,求分子,当然也可以求分母的 分

欧拉工程第65题:Convergents of e

题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the important mathematical constant,e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...]. The first ten terms in the sequence of convergents for e are: 2, 3,

欧拉工程第71题:Ordered fractions

题目链接:https://projecteuler.net/problem=71 If n<d and HCF(n,d)=1, it is called a reduced proper fraction. n/d 真分数升序排序后,离 3/7最近的数,d<=1000000 Java程序: public class P71{ void run(){ calculate(); } void calculate(){ int max_n = 1000000; long a = 3; long b

欧拉工程第73题:Counting fractions in a range

题目链接:https://projecteuler.net/problem=73 n/d的真分数 ,当d<=12000时 在 1/3 and 1/2 之间的有多少个 public class P73{ void run(){ FareySequences(); } void FareySequences(){ int limit = 12000; int a = 1; int b = 3; int c = 4000; int d = 11999; int count=0; while(!(c==

欧拉工程第51题:Prime digit replacements

题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到的质数是:56003, 56113, 56333, 56443, 56663, 56773, 和 56993.因此其中最小的56003就是具有这个性质的最小的质数. 找出最小的质数,通过用同样的数字置换其中的一部分(不一定是相邻的部分),能够得到八个质数. 解题思想: 这个题目是很难的 你首先要找到

欧拉工程第52题:Permuted multiples

题目链接 题目: 125874和它的二倍,251748, 包含着同样的数字,只是顺序不同. 找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字. 这个题目相对比较简单 暴力遍历 判断x,2x,3x,4x,5x,6x是否包含的数字相同 如何判断两个数包含的数字相同? 1. 两个数字转换成字符串后:d1,d2 定义两个集合ts1,ts2, 将d1,d2的各位数都添加到集合ts1中 将d1的各位数添加到集合ts2中 最后这个两个集合相等,则,这个两个数包含相同的数字 2.

欧拉工程第63题:Powerful digit counts

题目链接 这个题目有点坑: 先说自己的思路<虽然走不通> 根据题意可知道: a的b次方 除以 最小的b位数(如:10,100,1000) 的商 在 1--9之间,则:a的b次方就是符合题意的 然后就根据这个遍历 先找到第一个数符合条件的数firstnum 再找到第一个符合条件之后的第一个不满足条件的数nextnum 则:这中间有 nextnum - firstnum个数 当b也就是次方数大于18的时候,Long都溢出了 此时:有38个数