The Triangle 分类: 动态规划 算法 2014-10-18 22:13 86人阅读 评论(0) 收藏
2014-10-18 22:13
309 查看
The Triangle
时间限制:1000 ms | 内存限制:65535 KB难度:4
描述
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
输入Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but
<= 100. The numbers in the triangle, all integers, are between 0 and 99.
输出Your program is to write to standard output. The highest sum is written as an integer.
样例输入
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
样例输出
30
#include<stdio.h> int main(){ int n,i,j,len,sum,tem1,tem2; int a[101][101],s[101][101]; scanf("%d",&n); len=n; sum=0; for(i=1;i<=len;i++){ for(j=1;j<=i;j++){ scanf("%d",&a[i][j]); } } s[1][1]=a[1][1]; for(i=2;i<=len;i++){ for(j=1;j<=i;j++){ tem1=s[i-1][j-1]+a[i][j]; tem2=s[i-1][j]+a[i][j]; if(tem1>tem2){ s[i][j]=tem1; } else{ s[i][j]=tem2; } } } for(i=1;i<=len;i++) { if(s[len][i]>sum) sum=s[len][i]; } printf("%d",sum); return 0; }
相关文章推荐
- STL vector用法介绍 分类: 算法 2014-09-25 22:29 86人阅读 评论(0) 收藏
- 单调递增最长子序列 分类: 动态规划 算法 2014-10-29 13:11 74人阅读 评论(0) 收藏
- 超级台阶 分类: 算法 动态规划 2014-10-28 08:20 77人阅读 评论(0) 收藏
- 最长公共子序列 分类: 算法 动态规划 2014-10-27 21:50 72人阅读 评论(0) 收藏
- 线性表几个基本算法 分类: 编程 2013-03-19 09:26 171人阅读 评论(0) 收藏
- 递归求解整数划分问题 分类: 算法 2014-09-25 21:37 158人阅读 评论(0) 收藏
- 如何连接别人电脑上的ORACLE数据库 分类: 杂谈 2015-03-04 08:40 86人阅读 评论(0) 收藏
- String 匹配算法(2)---第32章 分类: 算法导论 2011-03-03 10:55 163人阅读 评论(0) 收藏
- 算法入门----第2章 分类: 算法导论 2011-03-03 11:11 131人阅读 评论(0) 收藏
- Palindrome Partitioning II 分类: Leetcode(动态规划) 2015-04-14 11:01 22人阅读 评论(0) 收藏
- 排序算法总结(1) 分类: 算法 2014-11-06 19:55 87人阅读 评论(0) 收藏
- String 匹配算法---第32章 分类: 算法导论 2011-03-03 10:43 193人阅读 评论(0) 收藏
- 求数组中出现1,2次数问题的讨论 分类: 算法 2014-12-06 09:28 81人阅读 评论(0) 收藏
- NYOJ-86 找球号(一)AC 分类: NYOJ 2014-02-02 10:45 160人阅读 评论(0) 收藏
- poj 3667 分类: poj templates 2015-03-30 22:13 31人阅读 评论(0) 收藏
- ACM的算法分类 2015-04-16 14:25 22人阅读 评论(0) 收藏
- 灰度世界算法(Gray World Algorithm) 分类: 图像处理 Matlab 2014-12-07 18:40 874人阅读 评论(0) 收藏
- 无线网络中常用的路由算法 分类: 协议 2014-10-08 19:40 86人阅读 评论(0) 收藏
- 归并排序 分类: 算法 2014-10-10 11:11 495人阅读 评论(0) 收藏
- 逻辑运算 分类: 算法 编程 2014-10-27 21:14 86人阅读 评论(0) 收藏