UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers【算术计算】

2021/8/3 23:06:40

本文主要是介绍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【算术计算】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程