当前位置:网站首页>【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;
}
}
}
边栏推荐
- [SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
- redis 哨兵集群搭建
- Latex merges multiple rows and columns of a table at the same time
- flex布局
- Implementation of PHP multitask second timer
- 基于消防GIS系统的智慧消防应用
- Jdbc流式查询与游标查询
- Workflow activiti5.13 learning notes (I)
- flex布局
- Understanding the mathematical essence of machine learning
猜你喜欢

逆序打印链表

平衡二叉树(AVL) ~

Leetcode:934. The shortest Bridge

金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))

H. Take the Elevator 贪心

Operating steps for uninstalling the mobile app

Jdbc流式查询与游标查询
C language explanation series - comprehensive exercises, guessing numbers games

Acquisition of bidding information

Convolutional neural network (III) - target detection
随机推荐
Can you make a JS to get the verification code?
金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
日志收集分析平台搭建-1-环境准备
Redis sentinel cluster setup
Learn about spark project on nebulagraph
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
字节面试题——判断一棵树是否为平衡二叉树
Flex layout
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
Convolutional neural network (IV) - special applications: face recognition and neural style transformation
Mysql45 talks about transaction isolation: why can't I see it after you change it?
知识沉淀一:架构师是做什么?解决了什么问题
vagrant下载速度慢的解决方法
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
[highly available MySQL solution] centos7 configures MySQL master-slave replication
Solution to slow download speed of vagrant
Sequential search, half search, block search~
Jdbc流式查询与游标查询
ETCD数据库源码分析——Cluster membership changes日志