当前位置:网站首页>[day_040421] binary conversion
[day_040421] binary conversion
2022-07-26 06:12:00 【On the Bank of Anhe Bridge】
Hexadecimal conversion
Title source
Cattle from : Hexadecimal conversion
Title Description
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 1
Input
7 2
Output
111
Thought analysis
- The basic idea of binary conversion is to take modules , Then divide by the base to convert , Until the result of division is 0
- Mark negative numbers ,0 Print results directly
- The result of division is greater than 10 Number of numbers , Use ABCD Equal letter representation , So use string Save the final result
- Define a string to store 0-F,16 A digital , The result of the calculation is mapped to the array to get the corresponding value
- No result is spliced into the string , Finally, reverse the string , And print the results
Code display
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int m = 0;
int n = 0;
string num = "0123456789ABCDEF";// Result set mapping table
while (cin >> m >> n)
{
string str = "";
//m by 0
if (m == 0)
{
cout << '0';
continue;
}
int flag = 0;// Mark positive and negative
if (m < 0)
{
flag = 1;
m = -m;
}
while (m)
{
// The method of binary conversion , modulus , Divide by the decimal number to convert , Until the result is 0
int temp = m % n;
str += num[temp];
m /= n;
}
if (flag)
{
str += '-';
}
// Inverse string
reverse(str.begin(), str.end());
cout << str << endl;
}
return 0;
}
边栏推荐
猜你喜欢

Blurring of unity pixel painting

Solutions to the failure of copy and paste shortcut keys

Matlab vector and matrix

Easycvr video square channel display and video access full screen display style problem repair

Viewing the technology stack of distributed system from the crash report of station B

unity 像素画导入模糊问题

Kingbasees SQL language reference manual of Jincang database (8. Function (10))

【(SV && UVM) 笔试面试遇到的知识点】~ phase机制

1.12 basis of Web Development

多目标检测
随机推荐
【Day_06 0423】不要二
招标信息获取
Leetcode:934. The shortest Bridge
unity 像素画导入模糊问题
Leetcode:336. palindrome pair
【pytorch】微调技术
Establishment of log collection and analysis platform-1-environment preparation
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
Understanding the mathematical essence of machine learning
Servlet filter details
数据库sql语言实战
flex布局
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
How to divide the disks under the devices and drives in win10 new computer
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
光量子里程碑:6分钟内解决3854个变量问题
Knowledge precipitation I: what does an architect do? What problems have been solved
redis 哨兵集群搭建
Webapi collation
Matlab vector and matrix