数塔 动规小练

数塔
原题链接https://vjudge.net/contest/349774#problem/N
在这里插入图片描述
在这里插入图片描述
我使用了一个结构体数组来记录每一层的数字,
然后dp加上上一层的两个数字取最大值,最后遍历最后一层输出最大值。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<fstream>
#include<queue>
using namespace std;
struct node
{
	long long a[105];
} stu[105];
long long dp[105][105];
int main()
{
	long long t;
	scanf("%lld",&t);
	while(t--)
	{
		long long n;
		scanf("%lld",&n);
		long long i,j;
		memset(stu,0,sizeof(stu));
		memset(dp,0,sizeof(dp));
		for(i=1; i<=n; i++)
		{
			for(j=1; j<=i; j++)
			{
				scanf("%lld",&stu[i].a[j]);
			}
		}
		dp[0][0]=0;
		long long z=0;
		for(i=1; i<=n; i++)
		{
			for(j=1; j<=i; j++)//遍历每一个位置,进行数据更新
			{
			//	printf("%lld %lld**\n",stu[i-1].a[j],stu[i-1].a[j-1]);
				dp[i][j]=max(dp[i-1][j]+stu[i].a[j],dp[i-1][j-1]+stu[i].a[j]);
			//	printf("%lld\n",j);
			}
		/*	cout<<"*************"<<endl;
			for(z=1; z<=n; z++)
			{
					printf("%lld ",dp[i][z]);
			}
			cout<<endl;
		}*/
	}
		long long maxx=-10086;
		for(i=1; i<=n; i++)
		{
			maxx=max(maxx,dp[n][i]);
		}
		printf("%lld\n",maxx);
	}
	return 0;
}
发布了130 篇原创文章 · 获赞 3 · 访问量 1608

猜你喜欢

转载自blog.csdn.net/yeyuluo/article/details/103919966
今日推荐