当前位置:网站首页>Blue Bridge Cup - binary conversion exercise
Blue Bridge Cup - binary conversion exercise
2022-07-24 11:08:00 【practical_ sharp】
Based on practice Decimal to hexadecimal
Problem description
Hexadecimal number is a kind of integer expression that is often used in program design . It has 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F common 16 Symbols , They represent decimal numbers respectively 0 to 15. The hexadecimal counting method is full 16 Into the 1, So decimal numbers 16 In hexadecimal it's 10, And decimal 17 In hexadecimal it's 11, And so on , Decimal 30 In hexadecimal it's 1E.
Give a nonnegative integer , Express it in hexadecimal form .
Input format
The input contains a non negative integer a, Represents the number to convert .0<=a<=2147483647
Output format
The output of this integer is 16 Hexadecimal said
The sample input
30
Sample output
1E
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
map<int,string>types;
int main()
{
types[0] = "0";types[1] = "1";types[2] = "2";types[3] = "3";
types[4] = "4";types[5] = "5";types[6] = "6";types[7] = "7";
types[8] = "8";types[9] = "9";types[10] = "A";types[11] = "B";
types[12] = "C";types[13] = "D";types[14] = "E";types[15] = "F";
string s16;
long long n;//n It's a decimal number , To turn into 16 Hexadecimal number
cin>>n;
// If n yes 0, Direct output 0 Exit procedure
if(n == 0){
cout<<0<<endl;
return 0;
}
// If n Less than 0, Output the minus sign first , Then deal with it as a positive number
if(n < 0){
cout<<"-";
n = -n;
}
// except 16 Remainder
while(n){
s16.insert(0,types[n%16]);// The remainder is inserted in the first place
n = n/16;
}
cout<<s16<<endl;
return 0;
}
Based on practice Hexadecimal to decimal
Problem description
Enter a number from the keyboard that does not exceed 8 A string of positive hexadecimal digits of , Convert it to a positive decimal number and output .
notes : In hexadecimal numbers 10~15 Use capital letters respectively A、B、C、D、E、F Express .
The sample input
FFFF
Sample output
65535
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
map<char,int>types;
int main()
{
types['0'] = 0;types['1'] = 1;types['2'] = 2;types['3'] = 3;
types['4'] = 4;types['5'] = 5;types['6'] = 6;types['7'] = 7;
types['8'] = 8;types['9'] = 9;types['A'] = 10;types['B'] = 11;
types['C'] = 12;types['D'] = 13;types['E'] = 14;types['F'] = 15;
string s16;//s16 yes 16 String form of hexadecimal number ,
long long n = 0;//n Is the decimal number converted , The initial value is set to 0
cin>>s16;
// If 0, Direct output 0 And quit the program
if(s16 == "0"){
cout<<0<<endl;
return 0;
}
// The hexadecimal input specified in the title is a positive number
for(int i=0;i<s16.length();i++){
n *= 16;// Position right Or rewrite it into n= n << 4;
n += types[s16[i]];// Add mantissa
}
cout<<n<<endl;
return 0;
}
Based on practice Hexadecimal to octal
Problem description
Given n Six positive hexadecimal integers , Output their corresponding octal numbers .
Input format
The first line of input is a positive integer n (1<=n<=10).
Next n That's ok , Each line is made up of 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Composed string , Represents the hexadecimal positive integer to convert , The length of each hexadecimal number does not exceed 100000.
Output format
Output n That's ok , Enter the corresponding octal positive integer for each line .
【 Be careful 】
The hexadecimal number you enter will not have a leader 0, such as 012A.
The output octal number also can't have leading 0.
The sample input
2
39
123ABC
Their thinking
The idea is to 16 Replace the hexadecimal number with a hexadecimal number , Then install and replace it with octal numbers .
Since the length of each hexadecimal number does not exceed 100000, Obviously not long long Deposit , It must all be in the form of strings .
that , You can replace hexadecimal numbers with binary numbers first , Then change from binary to octal .
Each hexadecimal digit corresponds to 4 Bit binary number , Every time 3 Bit binary number corresponds to one bit octal number .
among :
- Each hexadecimal 0-F Corresponding to binary 0000-1111
- Each octal 0-7 Corresponding to binary 000-111
- use map Store the mapping relationship between strings , For example, hexadecimal corresponds to binary "0"–>“0000”,“A”–>“1010”, Binary corresponds to octal "010"–>“2”
- Converted 0-1 character string ( Binary string ) If the length is not 3 Multiple , Insert a leading... At the beginning of its string 0 Until the length is 3 Multiple
- The last octal string installed and replaced should clear the leading 0, Until the first place is not the leader 0 Then output
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
// Hexadecimal to octal
// Method : Turn to binary first , Then convert to hexadecimal
string sixteento2(string s16)
{
string s2;
map<char,string>types;
types['0'] = "0000";types['1'] = "0001";types['2'] = "0010";types['3'] = "0011";
types['4'] = "0100";types['5'] = "0101";types['6'] = "0110";types['7'] = "0111";
types['8'] = "1000";types['9'] = "1001";types['A'] = "1010";types['B'] = "1011";
types['C'] = "1100";types['D'] = "1101";types['E'] = "1110";types['F'] = "1111";
for(int i=0;i<s16.length();i++){
;;
s2.append(types[s16[i]]);
}
return s2;
}
//2 Turn into the system 8 Base number
string doubleto8(string s2)
{
map<string,string>types;
types["000"] = "0";types["001"] = "1";types["010"] = "2";types["011"] = "3";
types["100"] = "4";types["101"] = "5";types["110"] = "6";types["111"] = "7";
string s8;
// If the binary string length is not 3 Multiple , Make up operation
if(s2.length()%3 == 1)
s2.insert(0,"00");
else if(s2.length()%3 == 2)
s2.insert(0,"0");
for(int i=0;i<=s2.length()-3;i=i+3){
s8.append(types[string(s2,i,3)]);
}
// Remove the possible leading 0
while(s8[0] == '0')
s8 = string(s8,1,s8.length()-1);
return s8;
}
int main()
{
int m;// Every conversion m Time
cin>>m;
for(int i=1;i<=m;i++){
string s;
cin>>s;
string sout = doubleto8(sixteento2(s));
cout<<sout<<endl;
}
return 0;
}
边栏推荐
- Web salted fish self rescue strategy -- typescript classes are not as difficult as you think
- Download path of twincat3 versions
- MySQL paging
- 爬虫与反爬:一场无休止之战
- Zero basis learning canoe panel (5) -- change the value of the variable, and the control image also changes. What's going on?
- 聊聊软件测试-自动化测试框架
- BBR and queuing
- This should be postman, the most complete interface testing tool in the whole network
- Self taught software testing talent -- not covered
- Use Modelsim to independently simulate Altera and Xilinx IP cores
猜你喜欢

Working principle and function application of frequency converter

Idea background image set

Altium one key automatic BOM

浅析拉格朗日乘数法及其对偶问题
![[FPGA]: IP core --divider (divider)](/img/bc/d8b7638e236c468ba23c8afc7ab70e.png)
[FPGA]: IP core --divider (divider)

LDR6028充电OTG直播线直播声卡音频转接器最具性价比方案

MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive

Fiddler抓包工具总结

【白帽子讲Web安全】第二章 浏览器安全

数据可视化-《白蛇2:青蛇劫起》(1)
随机推荐
How to convert word to markdown text
Redismission watchdog implementation mechanism can be understood at a glance
MySQL paging
Take care of me when I meet you for the first time
Research on parameter setting of MATLAB FFT
西门子200smart自创库与说明
read_ CSV error: 'GBK' codec can't decode byte 0xb4 in position 274: illegal multibyte sequence
Robot Framework官方教程(一)入门
【Golang】golang实现urlencode urldecode函数
Yum installation prompt to protect multi library version
Talk about new congestion control
Four components and working principle of frequency converter
[class, abstraction and inheritance]
[attack and defense world web] difficulty five-star 15 point advanced question: ics-07
Data visualization - White Snake 2: black snake robbery (1)
变频器的工作原理和功能应用
「低功耗蓝牙模块」主从一体 蓝牙嗅探-助力智能门锁
Zero basic learning canoe panel (9) -- combobox
Classification and introduction of arm and series processors
pip更新命令