20130820 【南华大学 ACM】 个人选拔赛第二场 【B . SUME】

来源:互联网 发布:微耕门禁数据库表结构 编辑:程序博客网 时间:2024/04/28 04:51

Problem B: SUME

Time Limit: 1 Sec  Memory Limit: 32 MB

Description

Once upon a time, there existed a sequence A  consisting of N positive integers. You don't know the
sequence itself, but you do know the sum of every two elements of the sequence. Find the sequence A!

Input

The first line of input contains the positive integer N (2 ≤ N ≤ 1000).
Each of the following N lines contains N positive integers smaller than or equal to 100 000, forming
the table S. The following relations hold: S(i, j) = A [i] + A [j] for i ≠ j, and S(i, j) = 0 for i = j. Here S(i,
j) denotes the number in the ith  row and jth column of the table, and A [i] denotes the ith element of the

sequence A.

It is guaranteed that for any input data set there exists a unique sequence of positive integers A with
the given properties.

Output

The   first   and   only   line   of   output   must   contain   the   required   sequence  A (in   the   form   of  N  space-
separated positive integers).

Sample Input

20 22 040 3 6 73 0 5 66 5 0 97 6 9 0

Sample Output

1 12 1 4 5

HINT


--------------------------------------------------------------------------------------------------------------------------


  1. 以第一行的数据为例( n 个元素),各元素为: 0, a1+a2, a1+a3, a1+a4, …… , a1+an;
  2. 显然,只要求出 a1, 那么 a2, a3, a4, …… ,an 都能一一求出。
  3. 求 a1 就需要 第二行 第三个元素( a[2][3] = a2 + a3 )
  4. a[1][3] - a[2][3] = a1 - a2 ;
  5. a[1][3] - a[2][3] + a[1][2] = 2 * a1 ;
  6. 所以: a1 = ( a[1][3] - a[2][3] + a[1][2] ) / 2 ;
  7. 接下来的,你懂的……
  8. 上述情况为 当 n>2 时。当 n=2 时,我就不懂了,如下情况:

    0 3

    3 0

会输出什么?( a1 = ?   ,   a2 = ?  )



--------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>int main(){int n,i,j,s[1010],ss[1010],a[1010],c;while( EOF != scanf("%d",&n) ){for(i=1;i<=n;i++)     // 第一行 数据scanf("%d",&s[i]);for(i=1;i<=n;i++)     // 第二行 数据scanf("%d",&ss[i]);if( 2==n ){           // 当 n=2 时a[1]=a[2]=s[2]/2;if( 2*a[1] != s[2] )a[2]=a[1]+1;printf("%d %d\n",a[1],a[2]);continue;}for(i=3;i<=n;i++)for(j=1;j<=n;j++)scanf("%d",&c);a[1] = a[i] = (s[3] - ss[3] + s[2] )/2;     // 求 a1printf("%d ",a[1]);                         // 输出 a1for(i=2;i<=n;i++){a[i] = s[i] - a[1];if( i!=n )printf("%d ",a[i]);elseprintf("%d\n",a[i]);}}return 0;}


原创粉丝点击