CF552-Div3-A. Restoring Three Numbers

解密三数之谜
本文介绍了一种解决数学谜题的方法,通过已知的四个数值(a+b, a+c, b+c, a+b+c),求解出三个未知正整数a、b、c的具体值。文章提供了一个简洁的C++代码实现,展示了如何通过排序和简单的数学运算来快速解决问题。

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

题意:给你三个数,这三个数分别是a+b,a+c,b+c,a+b+c(无序);求出a,b,c.(a>0,b>0,c>0)
题解:存进数组,sort以后,输出x4-x1,x4-x2,x4-x3(any order).x4是四个数中最大的数,可得出x4=a+b+c.那么其他三个数就是a+b,a+c,b+c.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int a[5];
    for(int i=1;i<=4;i++) cin>>a[i];
    sort(a+1,a+1+4);
    printf("%d %d %d\n",a[4]-a[1],a[4]-a[2],a[4]-a[3]);
    return 0;
}

转载于:https://www.cnblogs.com/-yjun/p/10727216.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值