您的位置:首页 > 其它

Restore 数学题,水题(转)

2016-04-06 21:24 357 查看
C - Restore

Time Limit:25000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

Gym 100735E

Description

standard input/output

Statements

Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are numbered from 0 to N-1. In this matrix, the sums of each row, the sums of each column, and the sum of the two diagonals are equal.

For example, a matrix with N = 3:

The sums of each row:

2 + 9 + 4 = 15

7 + 5 + 3 = 15

6 + 1 + 8 = 15

The sums of each column:

2 + 7 + 6 = 15

9 + 5 + 1 = 15

4 + 3 + 8 = 15

The sums of each diagonal:

2 + 5 + 8 = 15

4 + 5 + 6 = 15

As you can notice, all sums are equal to 15.

However, all the numbers in the main diagonal (the main diagonal consists of cells (i, i)) have been removed. Your task is to recover these cells.

Input

The first line contains the dimension of the matrix, n (1 ≤ n ≤ 100).

The following n lines contain n integers each, with removed numbers denoted by 0, and all other numbers  - 1012 ≤ Aij ≤ 1012.

Output

The restored matrix should be outputted in a similar format, with n lines consisting of n integers each.

Sample Input

Input

3

0 9 4

7 0 3

6 1 0

Output

2 9 4

7 5 3

6 1 8

这题就属于数学题了。。。有一个式子很容易想到,但是怎样把这个式子转化成我们需要的式子用了我好长时间。

下面用题目例子说明

0 9 4

7 0 3

6 1 0

sum1=0+9+4=13

sum1=7+0+3=10

sum1=6+1+0=7

a[1][1]=x1, a[2][2]=x2, a[3][3]=x3,

所以有sum1+x1==sum2+x2==sum3+x3==x1+x2+x3

其实这个式子很容易得到,下面就是放大招的时候了

sum1+x1+sum2+x2+sum3+x3==3(x1+x2+x3)

->sum1+sum2+sum3==2(x1+x2+x3)

->x1+x2+x3==(sum1+sum2+sum3)/2;

呃呃,这就能求出x1,x2,x3了。

#include <cstdio>
using namespace std;
long long  a[110][110];
long long row[110];
int n;

int main()
{
while(scanf("%d",&n)!=EOF)
{
long long  sum=0;
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%I64d",&a[i][j]);
row[i]+=a[i][j];
}
}
for(i=0; i<n; i++)
sum+=row[i];
sum/=(n-1);
for(i=0; i<n; i++)
a[i][i]=sum-row[i];
for(i=0; i<n; i++)
{
for(j=0; j<n-1; j++)
printf("%I64d ",a[i][j]);
printf("%I64d\n",a[i][n-1]);
}
}
}


转自同学博客:/article/7980672.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: