当前位置:网站首页>【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;
}
边栏推荐
- YOLOv6:又快又准的目标检测框架开源啦
- Cdga | how to build data asset catalogue?
- Database SQL language practice
- WebAPI整理
- Print linked list in reverse order
- Redis sentinel cluster setup
- YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
- Mysql45 talking about global lock, table lock and row lock
- em和rem
- Convolutional neural network (IV) - special applications: face recognition and neural style transformation
猜你喜欢

Full binary tree / true binary tree / complete binary tree~

Leetcode:940. How many subsequences have different literal values

What is spark serialization for?

PHP 多任务秒级定时器的实现方法

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

Understanding the mathematical essence of machine learning

数据库sql语言实战
![[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM](/img/19/ca3a5710ead3c5b9a222a8ae4ecb37.png)
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM

Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!

Youwei low code: Brick life cycle component life cycle
随机推荐
How can programmers improve mental internal friction?
光量子里程碑:6分钟内解决3854个变量问题
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
Understanding the mathematical essence of machine learning
C language explanation series - comprehensive exercises, guessing numbers games
知识沉淀一:架构师是做什么?解决了什么问题
[SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
K. Link with Bracket Sequence I dp
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
ETCD数据库源码分析——Cluster membership changes日志
JS的调用方式与执行顺序
2022年下半年系统集成项目管理工程师(软考中级)报名条件
How to divide the disks under the devices and drives in win10 new computer
Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
Cdga | how to build data asset catalogue?
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
【BM2 链表内指定区间反转】
[highly available MySQL solution] centos7 configures MySQL master-slave replication
H. Take the elevator greedy
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors