zhangmingsheng3521 @ 2023-08-26 10:16:37
rt.代码如下:
#include"bits/stdc++.h"
using namespace std;
int a[2002][2002];
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
for (int i=1;i<n;i++)
{
for (int j=1;j<=i;j++)
{
a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
}
}
printf("%d",a[1][1]);
return 0;
}
by 大眼仔Happy @ 2023-08-26 10:19:07
@zhangmingsheng3521 什么时候是水红了
by lujunxuan123 @ 2023-08-26 10:19:43
@zhangmingsheng3521 循环反了,要从下往上
by ImposterAnYu @ 2023-08-26 10:20:17
@zhangmingsheng3521 你从下往上走为啥不是倒着循环
by zhangzihan2029 @ 2023-08-26 10:21:54
把for (int i=1;i<n;i++)改为for(int i=n-1;i>=1;i--).你原先这样a[1][1]只能取a[2][1]与a[2][2]原值的max.
by 1q2zb @ 2023-08-26 10:24:31
#include"bits/stdc++.h"
using namespace std;
int a[2002][2002];
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
for (int i=n;i>=1;i--)
{
for (int j=i;j>=1;j--)
{
a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
}
}
printf("%d",a[1][1]);
return 0;
}
建议多检查for循环
此题问题:
1.i和j按你的dp公式应该是倒序的
2.要分清>、<、<=、>=
3.样例过了再提交
plz subscribe
by zhangmingsheng3521 @ 2023-08-26 10:26:12
@1q2zb 好的谢谢已关