Nyoj 18 其实也就是杭电上的数塔

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN= 110;

int MAX(int a, int b)
{
	return a > b ? a : b;
}

int main()
{
	int dp[MAXN][MAXN];
	int n;
	int i, j;
	while (~scanf("%d", &n))
	{
		memset(dp, 0, sizeof(dp));
		for (i = 0; i < n; ++i)
		{
			for (j = 0; j <= i; ++j)
			{
				cin>>dp[i][j];
			}
		}

		for (i = n-2; i >= 0; --i)
		{
			for (j = 0; j <= i; ++j)
			{
				dp[i][j] = MAX(dp[i][j]+dp[i+1][j], dp[i][j] + dp[i+1][j+1]);
			}
		}

		cout<<dp[0][0]<<endl;
	}
	return 0;
}

你可能感兴趣的:(dp)