#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 101
int D[MAX][MAX];
int * MaxSum;
int n;
void Solve_1() //递推
{
MaxSum = D[n];
for(int i = n-1; i>=1; --i)
for(int j = 1; j<=i; ++j)
MaxSum[j] = max(MaxSum[j],MaxSum[j+1]) + D[i][j];
cout << MaxSum[1] << endl;
}
int maxsum[MAX][MAX];
int Maxsum(int i,int j)
{
if(maxsum[i][j] != -1)
return maxsum[i][j];
if(i == n)
maxsum[i][j] = D[i][j];
else{
int x = Maxsum(i+1,j);
int y = Maxsum(i+1,j+1);
maxsum[i][j] = max(x,y) + D[i][j];
}
return maxsum[i][j];
}
void Solve_2() //递推
{
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= i; ++j)
maxsum[i][j] = -1;
cout << Maxsum(1,1);
}
int main()
{
cin >> n;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= i; ++j)
cin >> D[i][j];
Solve_2();
return 0;
}
数字三角形 递推和递归
最新推荐文章于 2022-07-30 18:44:03 发布