当前位置:网站首页>【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;
}
}
}
边栏推荐
- ament_cmake生成ROS2库并链接
- Convolutional neural network (III) - target detection
- 卸载手机自带APP的操作步骤
- ETCD数据库源码分析——Cluster membership changes日志
- VS中使用动态库
- 金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Acquisition of bidding information
- Practice operation and maintenance knowledge accumulation
- 时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
猜你喜欢

日志收集分析平台搭建-1-环境准备

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

Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system

Cdga | how to build data asset catalogue?

1.12 basis of Web Development

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

Convolutional neural network (II) - deep convolutional network: case study

Establishment of log collection and analysis platform-1-environment preparation

2022年下半年系统集成项目管理工程师(软考中级)报名条件

How to divide the disks under the devices and drives in win10 new computer
随机推荐
Intelligent fire protection application based on fire GIS system
How to divide the disks under the devices and drives in win10 new computer
Niuke network: TOPK problem of additive sum between two ordinal groups
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Sequential search, half search, block search~
Balanced binary tree (AVL)~
Should we test the Dao layer?
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
ament_cmake生成ROS2库并链接
Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
Is the transaction in mysql45 isolated or not?
金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情
英语句式参考纯享版 - 定语从句
金仓数据库 KingbaseES SQL 语言参考手册 (9. 常见DDL子句)
Properties of binary tree~
Ganglia installation and deployment process
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
H. Take the Elevator 贪心
vagrant下载速度慢的解决方法