__sunhy2012__ @ 2024-10-14 20:38:58
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[105][105],f[105][105],n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>a[i][j];
f[1][1]=a[1][1];
for(int i=2;i<=n;i++)
for(int j=2;j<=i;j++){
f[i][j]=max(f[i-1][j],f[i-1][j-1])+a[i][j];
cout<<f[i][j]<<" ";
}
cout<<endl;
int ans=1e9;
for(int i=1;i<=n;i++)
ans=max(ans,f[n][i]);
cout<<ans<<endl;
return 0;
}
by JoyLosingK @ 2024-10-14 20:43:29
@sunhy2012
#include<bits/stdc++.h>
using namespace std;
int a[1050][1050],f[1050][1050],n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>a[i][j];
f[1][1]=a[1][1];
for(int i=2;i<=n;i++)
for(int j=1;j<=i;j++)
f[i][j]=max(f[i-1][j],f[i-1][j-1])+a[i][j];
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,f[n][i]);
cout<<ans<<endl;
return 0;
}
改好了
by __sunhy2012__ @ 2024-10-14 20:57:55
@JoyLosingK 改哪了
by JoyLosingK @ 2024-10-14 21:01:18
by JoyLosingK @ 2024-10-14 21:01:39
@sunhy2012 求关
by __sunhy2012__ @ 2024-10-14 21:03:10
thanks 已关注,求互关
by tengyuxuan @ 2024-11-11 19:08:14
是max(f[i+1][j],[i+1][j+1]); 是看下面的
by tengyuxuan @ 2024-11-11 19:08:49
#include <bits/stdc++.h>
using namespace std;
int n;
int a[1010][1010];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cin>>a[i][j];
}
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
}
}
cout<<a[1][1];
return 0;
}