題解【CodeForces1154A】Restoring Three Numbers

Description

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\).ios

You have to guess three numbers \(a\), \(b\) and \(c\) using given numbers. Print three guessed integers in any order.c++

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 \(x_{1}\),\(x_{2}\),\(x_{3}\),\(x_{4}\)(\(2\)\(x_{i}\)\(10^{9}\)) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number \(x_{1}\),\(x_{2}\),\(x_{3}\),\(x_{4}\).dom

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

Input1:

3 6 5 4spa

Output1:

2 1 3code

Input2:

40 40 40 60排序

Output2:

20 20 20three

Input3:

201 101 101 200ip

Output3:

1 100 100

Solution

簡化版題意:

有三個正整數\(a\), \(b\), \(c\), 如今給定\(x_{1}\) \(=\) \(a + b\), \(x_{2}\) \(=\) \(a +c\), \(x_{3}\) \(=\) \(b + c\), \(x_{4}\) \(=\) \(a + b + c\)。 請求出\(a\), \(b\), \(c\)分別是多少。

這是一道數論題。

咱們先整理一下題面告訴咱們的信息:

  1. \(a\)\(b\)\(c\)是三個正整數;

  2. 咱們會輸入4個亂序的數字:\(x1\)\(x2\)\(x3\)\(x4\)

  3. \(x_{1}\) = \(a\) + \(b\) , \(x_{2}\) = \(a\) + \(c\) , \(x_{3}\) = \(b\) + \(c\) , \(x_{4}\) = \(a\) + \(b\) + \(c\)

\(a\)\(b\)\(c\)均>\(0\).

\(x_{4}\)是這四個數中最大的數。

至於如何求出\(a\)\(b\)\(c\),則能夠:

\(x_{4} - x_{1}\)獲得\(c\),用\(x_{4} - x_{2}\)獲得\(b\),用\(x_{4} - x_{3}\)獲得\(a\),最後按順序輸出這三個數便可。

注意:咱們須要開一個數組\(x\)[]來存儲\(x_{1}\)\(x_{2}\)\(x_{3}\)\(x_{4}\),由於這樣便於咱們排序(能夠直接調用\(c++\)庫函數\(sort\),但要開頭文件\(algorithm\)),並且能夠更好地幫助咱們尋找到\(a\)\(b\)\(c\)

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>//頭文件準備

using namespace std;//使用標準名字空間

inline int gi()//快速讀入,不解釋
{
    int f = 1, x = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return f * x;
}

int a, b, c, x[5];//a、b、c和x數組的意義同分析

inline void init()//分別輸入這四個數
{
    x[1] = gi(), x[2] = gi(), x[3] = gi(), x[4] = gi();
}

inline void solve()//將x數組從小到大排序
{
    sort(x + 1, x + 1 + 4);//1和4是指從x[1]到x[4]從小到大排序
}

inline void output()//輸出的自函數
{
    printf("%d %d %d\n", x[4] - x[1], x[4] - x[2], x[4] - x[3]);//分別輸出a、b、c。
}

int main()//進入乾淨整潔的主函數
{
    init();//輸入
    solve();//排序解決問題
    output();//輸出
    return 0;//養成return 0的好習慣
}
相關文章
相關標籤/搜索