POJ 2760: 数字三角形
2016-07-09 17:42
337 查看
import java.util.Scanner; public class Main { public static void main(String[] args) { int[][] triNumbers = new int[101][101];//0-100内 int[][] triMaxs = new int[101][101]; Scanner in = new Scanner(System.in); int nrow = in.nextInt();//行数 for(int i=0;i<nrow;i++)//行 { for(int j=0;j<=i;j++)//行内 { triNumbers[i][j] = in.nextInt(); } } in.close(); for(int i = nrow-1;i>=0;i--) { for(int j=i;j>=0;j--)//行内 { if(nrow==i) triMaxs[i][j] = triNumbers[i][j];//最后一行 else triMaxs[i][j] = Math.max(triMaxs[i+1][j], triMaxs[i+1][j+1])+triNumbers[i][j]; } } System.out.println(triMaxs[0][0]); } }
题目:
总时间限制: 1000ms 内存限制: 65536kB描述
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (图1)
图1给出了一个数字三角形。从三角形的顶部到底部有很多条不同的路径。对于每条路径,把路径上面的数加起来可以得到一个和,你的任务就是找到最大的和。
注意:路径上的每一步只能从一个数走到下一层上和它最近的左边的那个数或者右边的那个数。输入输入的是一行是一个整数N (1 < N <= 100),给出三角形的行数。下面的N行给出数字三角形。数字三角形上的数的范围都在0和100之间。输出输出最大的和。样例输入
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
样例输出
30
来源翻译自 IOI 1994 的试题一些些总结:This problem is adopted in Programming Guide. It adapts dynamic planning to solving the problem, which takes less space and time than the measure of recursion.Although the algorithm was described clearly, I still got "WA" when first pubmitted my src.After I compared the standard answer with mine , I found that the standard one adds 10 to the max boundary of the array.Well, after checking, I find in the circulation,i or j can be 101,so the max boundary must be no less than 101.The result always goes back on a careless man...
相关文章推荐
- NSURLSession学习笔记(二)Session Task
- Keil、uVision、MDK、Keil C51之间的区别比较
- NSURLSession学习笔记(一)简介
- [转] js == 与 === 的区别
- 几种傅里叶变换
- 小机房的树(codevs 2370)
- 4560 NOIP2015 D2T2 子串
- 说话有重点 测试思维
- LeetCode 215. Kth Largest Element in an Array
- 修改安卓中EditText中password模式下的符号
- ibatis和mybatis的区别
- 一个操作系统的实现(于渊)———前七章总结
- makefile 浅析(一)
- 服务调用效率比较
- javascript中,对于this指向的浅见
- html语法
- HBase伪分布式安装的简单步骤记录
- git服务器ubuntu下操作使用说明
- MFT的簇流数据结构详解
- React升级后带来的两个小问题及处理