zhuzihan0903 @ 2024-12-06 19:12:28
#include<bits/stdc++.h>
using namespace std;
int a[201][201],s[201][201];
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
s[i][j]=s[i-1][j]-s[i][j-1]-s[i-1][j-1]+a[i][j];
int r=-3000;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;i++){
for(int x=i;x<=n;i++){
for(int k=j;k<=n;i++){
r=max(r,s[x][k]-s[x][j-1]-s[i-1][k]+s[i-1][j-1]);
}
}
}
}
cout <<r<<endl;
return 0;
}
by niuniudundun @ 2024-12-06 19:36:08
@zhuzihan0903
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) s[i][j]=s[i-1][j]-s[i][j-1]-s[i-1][j-1]+a[i][j];
合并成一个。
by AIchenjunan @ 2024-12-15 11:20:13
#include <bits/stdc++.h>
using namespace std;
int mx=-99999999,n,d,a[1005][1005],s[1005][1005];
int main() {
cin>>n;
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
cin>>a[i][j];
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
for(int k=i; k<=n; k++) {
for(int l=j; l<=n; l++) {
if((s[k][l]-s[k][j-1]-s[i-1][l]+s[i-1][j-1])>mx) {
mx=s[k][l]-s[k][j-1]-s[i-1][l]+s[i-1][j-1];
}
}
}
}
}
cout<<mx;
return 0;
}
by LeavingAC @ 2024-12-29 22:06:54
@zhuzihan0903
1.前缀和是这么求的:
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
2.布什戈门你 4 重 for 循环都 i++
那能对吗。?!