数论 - Pairs(数字对)

In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live long, and never get Wrong Answers” . Everyone likes problems with short statements. Right? Let’s have five positive numbers: X1,X2,X3,X4,X5. We can form 10 distinct pairs of these five numbers. Given the sum of each one of the pairs, you are asked to find out the original five numbers.

Input

The first line will be the number of test cases T. Each test case is described in one line which contains 10 numbers, these are the sum of the two numbers in each pair. Notice that the input has no particular order, for example: the first number doesn’t have to be equal to {X1+ X2}. All numbers are positive integers below 100,000,000.

Output

For each test case, print one line which contains the number of the test case, and the five numbers X1,X2,X3,X4,X5 in ascending order, see the samples and follow the output format. There always exists a unique solution.

Examples
input
2
15 9 7 15 6 12 13 16 21 14
12 18 13 10 17 20 21 15 16 14
output
Case 1: 2 4 5 10 11
Case 2: 4 6 8 9 12 ------------------------------------------------------------------我是分割线^_^------------------------------------------------------------------ 题目的意思是原来有五个数,现在给定这些数两两相加的和,比如一开始的数为x1.x2.x3.x4.x5,那给定的十个和为x1+x2,
x1+x3,x1+x4.....x4+x5.....,就是这样,然后这题就是直接找一下基本就出来了,懵比的我偏偏还找了一个多小时,无语啊
实际上可以发现给定的十个数从小到大排序之后最小的和为x1+x2,最大的和为x4+x5,然后结合全局十个相加除以四为五
个数的和,就可以求出第三个数的了,求出第三个数之后再观察可以发现第二小的和是x1+x3,于是x1就求出来了,同理
可以得到x5(x3+x5是第二大的和),再重复观察就可全部求出来了,唉,真是平时不动脑筋不行呀= =
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
using namespace std; #define Int __int64
#define INF 0x3f3f3f3f typedef pair<int, int> pii; int main()
{
//freopen("input.txt", "r", stdin);
int n;
while (scanf("%d", &n) != EOF)
{
int sign = 1;
while (n--) {
int x[6] = {0};
int num[11];
int sum = 0;
for (int i = 1; i <= 10; i++) {
scanf("%d", &num[i]);
sum += num[i];
} sort(num + 1, num + 11); int s1 = sum / 4;
x[3] = s1 - num[1] - num[10];
x[1] = num[2] - x[3];
x[5] = num[9] - x[3];
x[2] = num[1] - x[1];
x[4] = num[10] - x[5];
printf("Case %d: ", sign++);
for (int i = 1; i <= 5; i++) {
printf("%d", x[i]);
if (i != 5) printf(" ");
else printf("\n");
}
}
}
return 0;
}

上一篇:UVa 122 Trees on the level(链式二叉树的建立和层次遍历)


下一篇:ThinkPHP学习遇到的点问题(学习中,持续更新)