18 The Triangle
来源:互联网 发布:自动软文软件 编辑:程序博客网 时间:2024/05/08 13:11
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.
- 样例输入
573 88 1 0 2 7 4 44 5 2 6 5
- 样例输出
30
#include<iostream>#include<cstdio>using namespace std;int main(){ int n,i,j,max; int a[101][101],dp[101][101]; scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<=i;j++) scanf("%d",&a[i][j]); max=0; dp[0][0]=a[0][0]; for(i=1;i<n;i++) { for(j=0;j<=i;j++) { if(j==0) { dp[i][j]=a[i][j]+dp[i-1][j]; continue; } if(j==i) { dp[i][j]=a[i][j]+dp[i-1][j-1]; continue; } if(dp[i-1][j-1]>dp[i-1][j]) dp[i][j]=a[i][j]+dp[i-1][j-1]; else dp[i][j]=a[i][j]+dp[i-1][j]; } } for(i=0;i<n;i++) if(max<dp[n-1][i]) max=dp[n-1][i]; printf("%d\n",max);}
0 0
- NYOJ 18 The Triangle
- NYOJ 18-The Triangle
- The Triangle(nyoj 18)
- nyoj-18-The Triangle
- NYOJ-18 The Triangle
- 18 The Triangle
- NYOJ 18 The Triangle
- The Triangle--nyoj 18
- nyoj 18The Triangle
- NYOJ 18 The Triangle
- NYOJ-18 The Triangle
- nyoj 18 The Triangle
- NYOJ 18 The Triangle
- 题目18:The Triangle
- 南阳 18 The Triangle(dp)
- NYOJ 题目18 The Triangle
- NYOJ 题目18 The Triangle
- The Triangle
- linux下的软件源安装+nfs挂载
- 17 单调递增最长子序列
- jdbc连接数据库
- Nest和三星推出智能家居网络标准Thread
- JS实现产生随机颜色的十六进制代码
- 18 The Triangle
- 博弈---威佐夫博奕(Wythoff Game)
- Java设计模式论述
- 22 素数求和问题
- JDBC注册驱动程序三种方式
- 要走得快,就一个人走;要走得远,就一起走
- 编程算法 - 不能被继承的类(模板参数友元) 代码(C++)
- 梵蒂冈梵蒂冈个
- Mongodb ODM: morphia