Restoring Three Numbers
Polycarp has guessed three positive integers a, b and c. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: a+b, a+c, b+c and a+b+c.
You have to guess three numbers a, b and c using given numbers. Print three guessed integers in any order.
Pay attention that some given numbers a, b and c can be equal (it is also possible that a=b=c).
Input
The only line of the input contains four positive integers x1,x2,x3,x4 (2≤xi≤109) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number x1,x2,x3,x4.
Output
Print such positive integers a, b and c that four numbers written on a board are values a+b, a+c, b+c and a+b+c written in some order. Print a, b and c in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.
Examples
input
3 6 5 4
output
2 1 3
input
40 40 40 60
output
20 20 20
input
201 101 101 200
output
1 100 100
题意 给你4个数,是随机排序的,但是符合a + b, a + c, b + c, a + b + c的4种其中一种,然后算出a,b,c的值,可以暴力枚举下情况下的复杂度是:
A
4
4
A^4_4
A44
做法 所以直接去写会写哭,因为排序的枚举排序结果也不多,所以有个模拟退火的思维可以使用,每次都随机,符合条件输出,控制在复杂度下,让退火速度尽量慢就行。
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
#define ll long long
ll q,w,e,r;
int T = 1e5;
int g[5],p[5];
int num;
int a,b,c;
int main(){
srand(time(NULL));
cin >> q >> w >> e >> r;
while(T--){
num = 0;
for(int i = 0; i < 4; i++) g[i] = 0,p[i] = 0;
num = 0;
while(num < 4){
int t = rand() % 4;
if(p[t] == 0){
p[t] = 1;
if(t == 0) g[num] = q;
else if(t == 1) g[num] = w;
else if(t == 2) g[num] = e;
else g[num] = r;
num++;
}
}
c = g[3] - g[0]; a = g[1] - c; b = g[2] - c;
if(a + b == g[0] && a + c == g[1] && b + c == g[2] && a + b + c== g[3]){
cout << a << " " << b << " " << c << endl;
break;
}
}
return 0;
}
/*
3 6 5 4
*/