欧拉工程第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)
欧拉工程第67题:Maximum path sum II的更多相关文章
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- [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 ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- 欧拉工程第55题:Lychrel numbers
package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...
- 欧拉工程第54题:Poker hands
package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...
- 欧拉工程第53题:Combinatoric selections
package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...
随机推荐
- Linux 网络工具之fping
系统中有个最最常用命令工具--ping,而fping 是ping 工具的加强版本.安装过程很简单: [root@mail /]# wget --limit-rate 500k http://www.f ...
- 【Qt】命令行编译Qt程序(nmake)【转】
简述 前两节讲解了如何在Visual Studio和Qt Creator中搭建Qt开发环境,并分享了我们第一个小程序-Hello World. 下面分享如何使用命令行来编译Qt程序.当然,MSVC和M ...
- silverlight 生成二维码
MainPage.xaml <Grid x:Name="LayoutRoot" Background="White"> <Border Bor ...
- mysql 的数据类型
mysql 的数据类型(描述的是字段)三大类:一.整型:1.tinyint(M),其中M是显示宽度,需要配合zerofill,就是前面0填充,存储单位为1个字节(8位),无符文是最大能存储范围0000 ...
- jquery 清空表达内容
function clearForm(objE) { $(objE).find(':input').each( function() { switch (this.type) { case 'pass ...
- 第六周 N题
Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (h ...
- JMS之开源实现ActiveMQ
1.ActiveMQ是开源的JMS实现. 可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给jms 服务端,而尽快的把屏幕返还给用户,且服务端能够多线程排队响应高并发的请求.可 ...
- MySQL 多实例数据库还原脚本-备份集与端口对应
版本:5.5.14 OS: ConetOS 6.3 1.创建recover.sh [root@yoon export]# vi recover.sh #!/bin/bash bakdir=/exp ...
- Flash制作遇到的小问题1--为何变形需要将图形打散(Ctrl+b)
今天上Flash实验课遇到一个小问题,就是我在画一个矩形如下图:
- sharepoint mysite and upgrade topics
My Sites overview (SharePoint Server 2010)http://technet.microsoft.com/en-us/library/ff382643(v=offi ...