nyoj18 The triangles 【DP】
2014-05-28 17:59
295 查看
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 a[101][101]; int max( int a, int b) { return a>b?a:b; } int main() { int n, i, j; while( ~scanf( "%d", &n ) ) { for( i = 0; i < n; i ++ ) for( j = 0; j <= i; j ++ ) scanf( "%d", &a[i][j] ); for( i = n-2; i >= 0; i -- ) for( j = i; j >= 0; j -- ) a[i][j] +=max( a[i+1][j], a[i+1][j+1] ); printf( "%d\n", a[0][0] ); } return 0; }
相关文章推荐
- nyoj18 The Triangle(dp)
- NYOJ 18-The Triangle(典型DP)
- NYOJ 18. The triangle(基础DP)
- NYOJ 18 The Triangle(简单dp)
- NYOJ题目18-The Triangle(经典dp)
- nyoj 18 The Triangle 【经典dp】
- NYOJ 18 The Triangle(基础dp)
- DP NYOJ 18
- nyoj--18--The Triangle(dp水题)
- nyoj--18--The Triangle(dp水题)
- NYOJ 18 The Triangle (dp问题)
- (NYoj 61) 传纸条(1) 动态规划,双向dp
- nyoj 214 单调递增子序列(二)【二分+DP】
- nyoj 325 (bfs +dp)
- nyoj 1070 诡异的电梯【Ⅰ】【dp】
- 【DP】NYOJ 571 整数划分(三)
- NYOj题目370波动序列(dp)
- NYOJ题目737石子合并(一)(区间dp)
- NYOJ 题目1023 还是回文(dp)
- NYOJ 76 超级台阶(基础dp)