Input
Output
Sample Input
1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
题目大意:中文题目。
解题思路:先记录最后一行的DP值,然后从最后一行开始往上层层递进。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int a[105][105], d[105][105]; int main() { int n; scanf("%d\n", &n); while (n--) { memset(a, 0, sizeof(a)); memset(d, 0, sizeof(d)); int m; scanf("%d\n", &m); int t = m; int cnt = 1; while (t--) { for (int i = 1; i <= cnt; i++) { scanf("%d", &a[cnt][i]); } cnt++; } for (int i = 1; i <= m; i++) { d[m][i] = a[m][i]; } for (int i = m - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { d[i][j] = a[i][j] + max(d[i + 1][j], d[i + 1][j + 1]); } } printf("%d\n", d[1][1]); } return 0; }
原文地址:http://blog.csdn.net/llx523113241/article/details/42685857