当前位置:网站首页>[programming training 4] calculate candy + hexadecimal conversion
[programming training 4] calculate candy + hexadecimal conversion
2022-07-18 13:17:00 【... running snail ~】
1. Calculate candy --》 link
【 describe 】
A,B,C Three people are good friends , Everyone has some candy in his hand , We don't know how many sweets each of them has , But we know the following information :
A - B, B - C, A + B, B + C.
These four values . Each letter represents the number of sweets each person has .
Now we need to calculate how many sweets each person has in his hand through these four values , namely A,B,C. There is guaranteed to be at most one set of integers A,B,C Meet all problem setting conditions .
Input description :
Enter as one line , altogether 4 It's an integer , Respectively A - B,B - C,A + B,B + C, Space off . The range is -30 To 30 Between ( Closed interval ).
Output description :
Output as a line , If there is an integer that satisfies A,B,C Then output... In sequence A,B,C, Space off , There is no space at the end of the line . If such an integer does not exist A,B,C, The output No
Example 1
Input : 1 -2 3 4
Output : 2 1 3
【 title 】:
A,B,C It's the amount of candy in three people's hands , We don't know A,B,C How much is the ? But we know that A - B, B - C, A + B, B + C Result , The result title is given to us by inputting test cases . So this problem is essentially an expression solving problem .
【 Their thinking 】:
1、A - B = a
2、B - C = b
3、A + B = c
4、B + C = d
The essence of this topic is : Judge whether there is a solution to the ternary system of first-order equations and solve it
1+3 You can get A=(a+c)/2;
4-2 You can get C=(d-b)/2;
2+4 You can get B2=(b+d)/2,
3-1 You can get B1=(c-a)/2;
If B1 Unequal B2 Then the expression has no solution
Code implementation :
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
int A=(a+c)/2;
int C=(d-b)/2;
int B1=(c-a)/2;
int B2=(b+d)/2;
if(B1!=B2)
cout<<"No";
else
cout<<A<<" "<<B1<<" "<<C;
return 0;
}
2. Hexadecimal conversion --》 link
【 describe 】
Given a decimal number M, And the base number to be converted N. Will decimal number M Turn into N Hexadecimal number
Input description :
Enter as one line ,M(32 An integer )、N(2 ≤ N ≤ 16), Space off .
Output description :
Output the converted number for each test instance , One line per output . If N Greater than 9, The corresponding number rules refer to 16 Base number ( such as ,10 use A Express , wait )
Example
Input : 7 2
Output : 111
【 Their thinking 】:
The idea is very simple , Take the mold (M%N), The remainder is the value of the current low base digit , Then by removing the hexadecimal number (M/=N), Enter the calculation of the next hexadecimal digit .
Code implementation :
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s,v="0123456789ABCDEF";
int m,n;
cin>>m>>n;
// If it's a negative number , Then it turns into a positive number , And mark it
bool flag=false;
if(m<0)
{
m=-m;
flag=true;
}
if(m==0)
cout<<'0'<<endl;
// Convert into corresponding characters by base and add them to s
while(m)
{
s+=v[m%n];
m/=n;
}
// Indicating starting m It's a negative number , Add the symbol to s in
if(flag)
s+='-';
reverse(s.begin(),s.end());// Inverted string
cout<<s<<endl;
return 0;
}
边栏推荐
猜你喜欢
随机推荐
Add right click to create a markdown file
Cadence learning path (VI) component packaging drawing
[programming compulsory training 9] the number of schemes in the grid (recursion) + alternative addition (bit operation)
This article enables you to understand IIC, SPI and UART protocols
SSH本地端口转发
03_案例搭建【RestTemplate 调用微服务】
HCIP Day 5 Notes
【RT-Thread】nxp rt10xx 设备驱动框架之--uart搭建和使用
金仓数据库 KingbaseES SQL语言参考手册(2. KingbaseES SQL介绍)
一边是旺旺丢不掉的童心 一边是放不下的功能饮料
[golang | GRC] GRC server streaming service end stream practice
Reversingkr WP (7)
智哪儿观察:鸿雁建博会推出全场景智慧解决方案,释放了什么信号
金仓数据库 KingbaseES SQL 语言参考手册 (3.1.1.13. JSON 类型)
Why is UDP header only 8 bytes
Jincang database kingbasees SQL language reference manual (3.1.1.6. boolean type, 3.1.1.7. bit string type)
【编程强训10】井字棋+密码强度等级
Rust iter& match
Viewpager conflict resolution
花生壳内网穿透实践指南







