【Codeforces】1230A – Dawid and Bags of Candies
Problem Description:
Dawid has four bags of candies. The i-th of them contains candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?
Note, that you can’t keep bags for yourself or throw them away, each bag should be given to one of the friends.
Input Specification:
The only line contains four integers , , and (1≤≤100) — the numbers of candies in each bag.
Output Specification:
Output YES if it’s possible to give the bags to Dawid’s friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase).
Sample Input1:
1 7 11 5
Sample Output1:
YES
Sample Input2:
7 3 2 5
Sample Output2:
NO
Note:
解题思路:
题目大意就是把4包糖果分给俩个朋友,看俩个朋友拿到的糖果数是否能相等。先将糖果进行升序排列,若最少的糖果数和最多的糖果数加起来刚好等于中间俩包糖果数,或者最多的糖果数刚好等于剩下的三包糖加起来的数量,就说明俩位朋友能拿到相等的糖果数输出YES,否则输出NO。
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int a[5];
Up(i,1,4)
{
cin >> a[i];
}
sort(a+1,a+5);
if(a[1]+a[4]==a[2]+a[3] || a[1]+a[2]+a[3]==a[4])
{
cout << "YES" << endl;
}
else cout << "NO" << endl;
return 0;
}
原文链接:【Codeforces】1230A - Dawid and Bags of Candies
麦芽雪冷萃 版权所有,转载请注明出处。
还没有任何评论,你来说两句吧!