ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVA10202 POJ2466 ZOJ1895 Pairsumonious Numbers【算术计算】

2021-08-03 23:00:50  阅读:174  来源: 互联网

标签:Pairsumonious int sum ++ vis flag ZOJ1895 Numbers 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;
}

标签:Pairsumonious,int,sum,++,vis,flag,ZOJ1895,Numbers,numbers
来源: https://blog.csdn.net/tigerisland45/article/details/119361009

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有