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)

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

  1. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

  2. [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 a ...

  3. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  4. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  5. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  6. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  7. 欧拉工程第55题:Lychrel numbers

    package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...

  8. 欧拉工程第54题:Poker hands

    package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...

  9. 欧拉工程第53题:Combinatoric selections

    package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...

随机推荐

  1. svn不能更新也不能提交【svn A conflict in the working copy obstructs the current operation】

    SVN不能提交解决方法:          最近发现了svn有一种特殊的冲突,跟svn版本库同步的时候,还提示代码没有不一样的,但是文件图标上又是一个特殊的冲突符号,不是那种大红的冲突符号.更新不了也 ...

  2. 完美解决fixed 水平居中问题

    群里的朋友问的,发现自己没写过:就写了下,原理和网上的fixed上下左右四个角的原理一样! 1.防止页面振动: body{ _background-image: url(about:blank); _ ...

  3. 【转载】DataGridView 使用集合作为数据源,并同步更新

    原文地址:http://hi.baidu.com/netyro/item/7340640e36738a813c42e239 今天做项目时遇到一个挠头的问题,当DataGridView的数据源为泛型集合 ...

  4. 【Qt】QSettings介绍【转】

    简介 QSettings类提供了持久的跨平台应用程序设置. 用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表,OS X和iOS的属性列表文件中. ...

  5. visual studio中创建单元测试

    1 打开  工具--自定义 2 选择 上下文菜单--编辑器上下文菜单|代码窗口 3 在这里我们可以看到“创建单元测试”这个菜单了,将它移到运行测试菜单下面 4 关闭VS并重启 重启后再对着类名,点击右 ...

  6. Android emulator warning----Emulator window was out of view and was recentred

    最近在打开Android emulator时,总会提示“Emulator window was out of view and was recentred ”,然后无法打开模拟器,但是可以使用Win7 ...

  7. Ubuntu12.10编译openwrt遇到的错误

    由于Openwrt有很多工具是要先编译的,在Ubuntu12.10平台下编译openwrt时就遇到了下面这样的错误:elf.cpp: In static member function 'static ...

  8. find命令小结

    find命令小结 find命令用于在系统中查找文件,配合 -exec 选项或 xargs命令还能对查找到得文件执行一些列的自动化操作. 基本格式:find [-H] [-L] [-P] [path.. ...

  9. snmptrap使用

    SNMP简单网络管理协议,其中其支持的一个命令snmptrap命令,用于模拟向管理机发送trap消息.   启动陷阱方法: snmptrapd -C -c /etc/snmp/snmptrapd.co ...

  10. Notes of the scrum meeting(2013/10/23)

    ps:本来是10月23号周三下午开的会,这几天由于各种事情忙,忘记写博客了,现在补上. 软工项目组buaa_smile开始项目第一次scrum meeting meeting time:4:00~5: ...