zoukankan      html  css  js  c++  java
  • Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    E - Restore

    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.

    Example

    Input
    3
    0 9 4
    7 0 3
    6 1 0
    Output
    2 9 4 
    7 5 3
    6 1 8

     
    这个题有一个地方有点坑,就是如果一行里面有两个0,因为只有对角线的挖掉了,所以直接竖着算这一排的就可以。其他的没了。
     
    代码:
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 ll a[105][105];
     8 int main(){
     9     int n;
    10     while(~scanf("%d",&n)){
    11             ll cnt=0;
    12         for(int i=0;i<n;i++){
    13             for(int j=0;j<n;j++){
    14                 scanf("%lld",&a[i][j]);
    15                 cnt+=a[i][j];
    16             }
    17         }
    18         cnt/=n-1;int i,j;   //算那个相等的值
    19         for(i=0;i<n;i++){
    20                 ll num=0;int flag,ret=0;
    21             for(j=0;j<n;j++){
    22                 num+=a[i][j];
    23                 if(a[i][j]==0){flag=j;ret++;}
    24             }
    25             if(ret>1){    //如果这一排有多于1个0
    26                 ll qwe=0;
    27                 for(int k=0;k<n;k++)qwe+=a[k][i];
    28                 a[i][i]=cnt-qwe;
    29             }
    30             else if(num!=cnt)a[i][flag]=cnt-num;
    31         }
    32         for(int i=0;i<n;i++){
    33             for(int j=0;j<n;j++){
    34                 printf("%lld",a[i][j]);
    35                 if(j!=n-1)printf(" ");
    36                 else printf("
    ");
    37             }
    38         }
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    Makefile 文件格式
    带你深入理解传递參数
    LeetCode OJ Basic Calculator II
    emacs 为什么找不到运行程序?
    iOS Dev (51)加急审核
    POJ 1159 Palindrome
    9.9递归和动态规划(十二)——小鸡吃米
    c++操作当前窗体句柄
    [Python]计算闰年时候出现的and和or优先级的问题以及短路逻辑
    静态代理模式
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9703197.html
Copyright © 2011-2022 走看看