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 }
  • 相关阅读:
    Java中的 多线程编程
    Python中的字典详解
    Python中的数据类型
    Python中的字符串操作总结(Python3.6.1版本)
    R语言绘制沈阳地铁线路图
    HIVE中的order by操作
    Hive中order by,sort by,distribute by,cluster by的区别
    HDFS shell命令
    HDFS入门
    Bootstrap_Datatable Ajax请求两次问题的解决
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9703197.html
Copyright © 2011-2022 走看看