//枚举第一个,暴力过
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a[5][5];
int main()
{
int i,j,k;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d",&a[i][j]);
for(int i=1;;i++)
{
int sum=i+a[1][2]+a[1][3];
j=sum-a[2][1]-a[2][3];
k=sum-a[3][1]-a[3][2];
if(j>0&&k>0&&i+j+k==sum)
{
a[1][1]=i;
a[2][2]=j;
a[3][3]=k;
break;
}
}
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
printf("%d%c",a[i][j],j==3?'\n':' ');
return 0;
}
//很巧妙的方法,朋友写的
#include<stdio.h>
int main()
{
int a[5][5];
int i,sum=0,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
{
scanf("%d",&a[i][j]);
sum+=a[i][j];
}
sum/=2;
a[1][1]=sum-a[1][2]-a[1][3];
a[2][2]=sum-a[2][1]-a[2][3];
a[3][3]=sum-a[3][1]-a[3][2];
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d%c",a[i][j],j==3?'\n':' ');
}
}
return 0;
}
Codeforces Round #157 (Div. 2)——B
最新推荐文章于 2023-07-27 18:56:29 发布