欧拉工程第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 ...
 
随机推荐
- Andriod docs加载速度慢的问题解决
			
网上找了个类, import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import ja ...
 - Linux下iftop网卡流量监控使用
			
在类linux系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.i ...
 - 【转】IL编织 借助PostSharp程序集实现AOP
			
ref: C# AOP实现方法拦截器 在写程序的时候,很多方法都加了.日志信息.比如打印方法开始,方法结束,错误信息,等等. 由于辅助性功能的代码几乎是完全相同的,这样就会令同样的代码在各个函数中 ...
 - 封装鼠标滚轮事件_mousewheel
			
function mousewheel(obj,fn){ obj.onmousewheel===null ? obj.onmousewheel=fun : obj.addEventListener(' ...
 - PHP中文URL编解码(urlencode()rawurlencode()
			
PHP中对于URL进行编码,可以使用 urlencode() 或者 rawurlencode(),二者的区别是前者把空格编码为 '+',而后者把空格编码为 '%20',不过应该注意的是,在编码时应该只 ...
 - 当年的笔记_apache配置虚拟主机
			
下午需要,在网上找了一堆,没找到合适的,翻出来自己当年的笔记,还是自己记的容易理解. 解决方案1:通过端口来区分 1>添加一个虚拟主机1.在d盘下新建www目录,如:d:/www. 2.修改ht ...
 - C# 链接Sql和Access数据库语句
			
1.sql数据库: 1.1.链接数据语句:server=localhost;database=Data; uid=sa;pwd=123; 或 Data Source=localhost;DataBas ...
 - 机器学习实战——k-邻近算法:约会网站
			
1.kNN 算法 算法说明: set<X1,X2……Xn> 为已知类别数据集,预测 点Xt 的类别: (1)计算中的set中每一个点与Xt的距离 (2)按距离增序排列 (3)选择距离最小的 ...
 - MySQL通过Binlog恢复删除的表
			
查看log-bin是否开启:mysql> show variables like '%log%bin%';+---------------------------------+-------+| ...
 - 如何正确理解深度学习(Deep Learning)的概念
			
现在深度学习在机器学习领域是一个很热的概念,不过经过各种媒体的转载播报,这个概念也逐渐变得有些神话的感觉:例如,人们可能认为,深度学习是一种能够模拟出人脑的神经结构的机器学习方式,从而能够让计算机具有 ...