题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2084
题目大意,有一组数字,以三角的形式排列。现从该数字三角的顶端走到底层,需要所走路径的数字和最大,并输出该值。
ac代码
#include<cstdio>
#include<iostream>
using namespace std;
const int M = 10000;
int shuta[M][M];
int dp[M];
int max(int x, int y)
{
if(x > y)return x;
else return y;
}
int main()
{
int T;
scanf("%d", &T);
for(int t = 1; t <= T; t++)
{
int deep;
scanf("%d", &deep);
for(int i = 1; i<= deep; i++)
for(int j = 1; j <= i; j++)
scanf("%d", &shuta[i][j]);
for(int i = 1; i <= deep; i++)
dp[i] = shuta[deep][i];
for(int i = deep - 1; i >= 1; i--)
for(int j = 1; j <= i; j++)
dp[j] = max(dp[j], dp[j + 1]) + shuta[i][j];
printf("%d\n", dp[1]);
}
return 0;
}