Problem B: SUME
Time Limit: 1 Sec Memory Limit: 32 MBDescription
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
2 0 2 2 0 4 0 3 6 7 3 0 5 6 6 5 0 9 7 6 9 0
Sample Output
1 1 2 1 4 5
HINT
--------------------------------------------------------------------------------------------------------------------------
- 以第一行的数据为例( n 个元素),各元素为: 0, a1+a2, a1+a3, a1+a4, …… , a1+an;
- 显然,只要求出 a1, 那么 a2, a3, a4, …… ,an 都能一一求出。
- 求 a1 就需要 第二行 第三个元素( a[2][3] = a2 + a3 )
- a[1][3] - a[2][3] = a1 - a2 ;
- a[1][3] - a[2][3] + a[1][2] = 2 * a1 ;
- 所以: a1 = ( a[1][3] - a[2][3] + a[1][2] ) / 2 ;
- 接下来的,你懂的……
- 上述情况为 当 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; // 求 a1
printf("%d ",a[1]); // 输出 a1
for(i=2;i<=n;i++){
a[i] = s[i] - a[1];
if( i!=n ) printf("%d ",a[i]);
else printf("%d\n",a[i]);
}
}
return 0;
}