UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers算术计算

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers算术计算相关的知识,希望对你有一定的参考价值。

Pairsumonious Numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1064 Accepted: 521 Special Judge

Description

For 10 > N > 2 numbers we form N*(N-1)/2 sums by adding every pair of the numbers. Your task is to find the N numbers given the sums.

Input

Each line of input contains N followed by N*(N-1)/2 integer numbers separated by a space.

Output

For each line of input, output one line containing N integers in non-descending order such that the input numbers are pairwise sums of the N numbers. If there is more than one solution, any one will do; if there is no solution, print “Impossible”.

Sample Input

3 1269 1160 1663
3 1 1 1
5 226 223 225 224 227 229 228 226 225 227
5 216 210 204 212 220 214 222 208 216 210
5 -1 0 -1 -2 1 0 -1 1 0 -1
5 79950 79936 79942 79962 79954 79972 79960 79968 79924 79932

Sample Output

383 777 886
Impossible
111 112 113 114 115
101 103 107 109 113
-1 -1 0 0 1
39953 39971 39979 39983 39989

Source

Waterloo local 2001.09.29

问题链接UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers
问题简述:给定n和n个数的两两相加之和,求原先的n个数。
问题分析:算术计算问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers */

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 10;
const int M = N * (N - 1) / 2;
int a[N], sum[M], vis[M];

int main()
{
    int n;
    while (~scanf("%d", &n)) {
        int m = n * (n - 1) / 2;
        for (int i = 0; i < m; i++) scanf("%d", &sum[i]);

        sort(sum , sum + m);

        int i;
        for (i = 2; i < m; i++) {
            a[1] = (sum[0] + sum[1] - sum[i]) / 2;
            a[2] = sum[0] - a[1];
            a[3] = sum[1] - a[1];
            if (a[2] + a[3] == sum[i]) {
                memset(vis, 0, sizeof vis);
                vis[i] = 1;
                int s = 2;
                bool flag = true;
                for (int j = 4; j <= n && flag; j++) {
                    while (vis[s]) s++;
                    a[j] = sum[s] - a[1];
                    vis[s] = 1;
                    for (int k = 2; k < j && flag; k++) {
                        int l;
                        for (l = s + 1; l < m && flag; l++)
                            if (vis[l] == 0 && a[j] + a[k] == sum[l]) {
                                vis[l] = 1;
                                break;
                            }
                        if (l >= m) flag = false;
                    }
                }
                if (flag) break;
            }
        }

        if (i >= m) printf("Impossible\\n");
        else {
            for (int j = 1; j <= n; j++) {
                if (j > 1) printf(" ");
                printf("%d", a[j]);
            }
            printf("\\n");
        }
    }

    return 0;
}

以上是关于UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers算术计算的主要内容,如果未能解决你的问题,请参考以下文章

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA10670 POJ1907 ZOJ2372 Work Reduction贪心

UVA10081 POJ2537 ZOJ1883 Tight WordsDP

UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学

POJ2689 ZOJ1842 UVA10140 Prime Distance筛选法

POJ2351 ZOJ1916 UVA10371 Time Zones时区计算