动态规划 简单题dp

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

输入n,接着n行,n<=200

sample input:

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

output :

30

#include<iostream>
using namespace std;
int main()
{
int n,a[210],t[210]={0},i,j;
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cin>>a[j];
for(j=i;j>=1;j--)
t[j]=a[j]+(t[j-1]>t[j]? t[j-1]:t[j]);
}
j=0;
for(i=1;i<n;i++)
if(j<t[i])
j=t[i];
cout<<j;
return 0;
}
原文地址:https://www.cnblogs.com/bersaty/p/2249073.html