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 }
  • 相关阅读:
    Canvas技术
    线段树树状数组小结
    html5 Canvas元素
    ccpc网络邀请赛部分
    2020杭电多校
    JavaFx 绘制图形和文本(笔记)
    P4094 [HEOI2016/TJOI2016]字符串(二分+多种数据结构)
    L3-021 神坛 (30分) (计算几何最小三角形面积)
    10月28日G、H、I题
    出栈序列的合法性 (25分) 之树状数组玄学做法
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9703197.html
Copyright © 2011-2022 走看看