Kaden_ @ 2022-07-03 16:24:00
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=1000;
int a[maxn][maxn],f[maxn][maxn];
int main()
{
int n,i,j;
scanf("%d",&n);//金字塔层数
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
f[1][1]=a[1][1];
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
f[i][j]=max(f[i-1][j-1],f[i-1][j])+a[i][j];
}
}
int sum=0;
for(i=1;i<=n;i++)
{
sum=max(sum,f[n][i]);
}
printf("%d",sum);
return 0;
}
by KAqwq @ 2022-07-03 16:29:11
把 const int maxn=1000
改为 const int maxn=1005
即可,数据 maxn=1000
时下标会越界
by Kaden_ @ 2022-07-03 16:34:22
@Kamisato_Ayato 过了,谢谢