用一个二维数组记录状态的值,避免重复计算
#include<iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
#define MAX 105
int D[MAX][MAX];
int MaxNum[MAX][MAX];
int n;
int CalMax(int i,int j)
{
if(MaxNum[i][j]!=-1)
return MaxNum[i][j];
if(i==n)
{
MaxNum[i][j] = D[i][j];
return MaxNum[i][j];
}
else
{
int x = CalMax(i+1,j);
int y = CalMax(i+1,j+1);
MaxNum[i][j] = D[i][j] + max(x,y);
}
return MaxNum[i][j];
}
int main()
{
cin >> n;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
{
cin >> D[i][j];
MaxNum[i][j] = -1;
}
cout << CalMax(1,1)<<endl;
return 0;
}