Restore 数学题,水题(转)

4 篇文章 0 订阅
2 篇文章 0 订阅

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]);
        }
    }
}

转自同学博客:http://blog.csdn.net/u011613367/article/details/51040207

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值