当前位置:网站首页>【Day_04 0421】计算糖果
【Day_04 0421】计算糖果
2022-07-26 06:08:00 【安河桥畔】
计算糖果
题目来源
牛客网:计算糖果
题目描述
A,B,C三个人是好朋友,每个人手里都有一些糖果,我们不知道他们每个人手上具体有多少个糖果,但是我们知道以下的信息:
A - B, B - C, A + B, B + C. 这四个数值.每个字母代表每个人所拥有的糖果数.
现在需要通过这四个数值计算出每个人手里有多少个糖果,即A,B,C。这里保证最多只有一组整数A,B,C满足所有题设条件。
输入描述
输入为一行,一共4个整数,分别为A - B,B - C,A + B,B + C,用空格隔开。 范围均在-30到30之间(闭区间)。
输出描述
输出为一行,如果存在满足的整数A,B,C则按顺序输出A,B,C,用空格隔开,行末无空格。 如果不存在这样的整数A,B,C,则输出No
示例1
输入
1 -2 3 4
输出
2 1 3
思路分析
- 根据数学中解三元一次方程组的方法,加减消元,分别算出A、B、C的值
- 由于B的解法不止一种,所以用两种方法求出B的值比较,保证只有一种解
- 三人拥有的的糖果数目大于等于0
代码展示
#include<iostream>
using namespace std;
int main()
{
int n1, n2, n3, n4;
int a, b1, b2, c;
//循环输入多个数据
while (cin >> n1 >> n2 >> n3 >> n4)
{
if (n3 < 0 || n4 < 0)
{
cout << "No";
continue;
}
//糖果数目不为小数
if ((n1 + n3) % 2 != 0 || (n2 + n4) % 2 != 0 || (n3 - n1) % 2 != 0 || (n4 - n2) % 2 != 0)\
{
cout << "No";
continue;
}
a = (n1 + n3) / 2;
b1 = (n2 + n4) / 2;
b2 = (n3 - n1) / 2;
c = (n4 - n2) / 2;
if (b1 != b2 || a < 0 || b1 < 0 || c < 0)
{
cout << "No";
}
else
{
cout << a << " " << b1 << " " << c;
}
}
}
边栏推荐
- Embedded sharing collection 15
- Matlab vector and matrix
- Introduction of four redis cluster schemes + comparison of advantages and disadvantages
- 某公司给每个工位装监控:只为看员工写代码?
- Widget is everything, widget introduction
- Mysql45 talking about infrastructure: how is an SQL query executed?
- Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
- Calling mode and execution sequence of JS
- flex布局
- 1.12 Web开发基础
猜你喜欢

Mysql45 talking about global lock, table lock and row lock

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明

Cdga | how to build data asset catalogue?

满二叉树 / 真二叉树 / 完全二叉树 ~

Amd zen4 game God u reached 208mb cache within this year, which is unprecedented

redis 哨兵集群搭建

平衡二叉树(AVL) ~

基于消防GIS系统的智慧消防应用

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4

Mysql45 talks about transaction isolation: why can't I see it after you change it?
随机推荐
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
【Oracle SQL】计算同比与环比(列转行进行偏移)
Servlet无法直接获取request请求中的JSON格式数据
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
Docking wechat payment (II) unified order API
ament_ Cmake generates the ros2 library and links it
Solutions to the failure of copy and paste shortcut keys
Ros2 preliminary: basic communication with topic
Operating steps for uninstalling the mobile app
二叉树的前中后序遍历——本质(每个节点都是“根”节点)
Flex layout
Knowledge precipitation I: what does an architect do? What problems have been solved
Distributed | practice: smoothly migrate business from MYCAT to dble
Recursive processing - subproblem
金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
H. Take the elevator greedy
Unity2d animator cannot create transition
L. Link with Level Editor I dp
Convolutional neural network (III) - target detection