题意:

将0换为任意正整数使矩阵的每一行的和和每一列的和以及正反对角线的和都相等。

这场没有上分的罪魁祸首!!!!!!

注意两个特判:

当n==1时;

当结果res<=0时;

附AC代码:

 #include<bits/stdc++.h>
using namespace std; long long a[][];
long long sumx[];
long long sumy[]; int main(){
int n;
cin>>n; int x,y;
memset(sumx,,sizeof(sumx));
memset(sumy,,sizeof(sumy));
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
cin>>a[i][j];
sumx[i]+=a[i][j];
sumy[j]+=a[i][j];
if(a[i][j]==){
x=i;
y=j;
}
}
}
if(n==){
cout<<""<<endl;
return ;
}
long long res;
if(x!=){
res=sumx[]-sumx[x];
}
else{
res=sumx[]-sumx[x];
}
sumx[x]+=res;
sumy[y]+=res;
a[x][y]=res;
long long sums=,sumt=;
for(int i=;i<=n;i++){
sums+=a[i][i];
sumt+=a[i][n-i+];
}
sort(sumx+,sumx+n+);
sort(sumy+,sumy+n+);
// cout<<sumx[1]<<" "<<sumx[n]<<" "<<sumy[1]<<" "<<sumy[n]<<" "<<sums<<" "<<sumt<<" "<<res;
if(sumx[]==sumx[n]&&sumy[]==sumy[n]&&sumx[]==sumy[n]&&sums==sumt&&sums==sumx[]&&res>){
cout<<res<<endl;
}
else{
cout<<"-1"<<endl;
}
return ;
}
04-18 01:20