当前位置:网站首页>【Day_04 0421】进制转换
【Day_04 0421】进制转换
2022-07-26 06:08:00 【安河桥畔】
进制转换
题目来源
牛客网:进制转换
题目描述
给定一个十进制数M,以及需要转换的进制数N。将十进制数M转化为N进制数
输入描述
输入为一行,M(32位整数)、N(2 ≤ N ≤ 16),以空格隔开。
输出描述
为每个测试实例输出转换后的数,每个输出占一行。如果N大于9,则对应的数字规则参考16进制(比如,10用A表示,等等)
示例1
输入
7 2
输出
111
思路分析
- 进制转换的基本思路是取模,然后除以要转换的进制,直到除的结果为0
- 对负数进行标记,0直接打印结果
- 除的结果有大于10的数,要用ABCD等字母表示,所以采用使用string保存最终的结果
- 定义一个字符串存放0-F,16个数字,计算的结果映射到数组中得到对应的值
- 没得到一个结果拼接到字符串中,最后将字符串逆置,并打印结果
代码展示
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int m = 0;
int n = 0;
string num = "0123456789ABCDEF";//结果集映射表
while (cin >> m >> n)
{
string str = "";
//m为0
if (m == 0)
{
cout << '0';
continue;
}
int flag = 0;//标记正负
if (m < 0)
{
flag = 1;
m = -m;
}
while (m)
{
//进制转换的方法,取模,除以要转换的进制数,直到结果为0
int temp = m % n;
str += num[temp];
m /= n;
}
if (flag)
{
str += '-';
}
//逆置字符串
reverse(str.begin(), str.end());
cout << str << endl;
}
return 0;
}
边栏推荐
- 二叉树的前中后序遍历——本质(每个节点都是“根”节点)
- Intelligent fire protection application based on fire GIS system
- Can you make a JS to get the verification code?
- Understanding the mathematical essence of machine learning
- 【2023杰理科技提前批笔试题】~ 题目及参考答案
- [2023 Jerry technology approval test questions in advance] ~ questions and reference answers
- Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
- Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
- Talking about the practice of software defect management
- 递归处理——子问题
猜你喜欢

Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism

数据库sql语言实战

软件测试面试题全网独家没有之一的资深测试工程师面试题集锦

Recursive processing - subproblem

Widget is everything, widget introduction

移动web

Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument

Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)

分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
随机推荐
Flex layout
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
Ganglia installation and deployment process
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
2022年下半年系统集成项目管理工程师(软考中级)报名条件
移动web
Mysql45 talking about infrastructure: how is an SQL query executed?
Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
Using dynamic libraries in VS
Flex layout
Optical quantum milestone: 3854 variable problems solved in 6 minutes
Operating steps for uninstalling the mobile app
数据库sql语言实战
[free and easy to use] holiday query interface
招标信息获取
Balanced binary tree (AVL)~
Calling mode and execution sequence of JS
Mysql45 speak in simple terms index
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
Recursive processing - subproblem