当前位置:网站首页>AcWing 3438. 数制转换
AcWing 3438. 数制转换
2022-07-03 17:16:00 【永远有多远.】
求任意两个不同进制非负整数的转换(22 进制 ∼∼ 1616 进制),所给整数在 int 范围内。
不同进制的表示符号为(0,1,…,9,a,b,…,f0,1,…,9,a,b,…,f)或者(0,1,…,9,A,B,…,F0,1,…,9,A,B,…,F)
输入格式
输入只有一行,包含三个整数 a,n,b。a 表示其后的 n 是 a 进制整数,b 表示欲将 a 进制整数 nn 转换成 bb 进制整数。
a,ba,b 是十进制整数。
数据可能存在包含前导零的情况。
输出格式
输出包含一行,该行有一个整数为转换后的 bb 进制数。
输出时字母符号全部用大写表示,即(0,1,…,9,A,B,…,F0,1,…,9,A,B,…,F)。
数据范围
2≤a,b≤162≤a,b≤16,
给定的 a 进制整数 n 在十进制下的取值范围是 [1,2147483647][1,2147483647]。
输入样例:
15 Aab3 7
输出样例:
210306
题目大意:
将a进制整数n转换为b进制整数
思路:
以10进制为中心 先将a进制转换为10进制 再把转换后的十进制转换为b进制
任意进制数转换为10进制
int toTen(int a, string num) //a进制数num转换为10进制
{
int ans = 0;
int n = num.size();
for (int i = 0; i < num.size(); i++) {
if (isupper(num[i]))
ans += pow(a, n - i - 1) * (num[i] - 'A' + 10);
else if (islower(num[i]))
ans += pow(a, n - i - 1) * (num[i] - 'a' + 10);
else if (isdigit(num[i]))
ans += pow(a, n - i - 1) * (num[i] - '0');
}
return ans;
}
十进制数转任意进制
unordered_map<int, char> mp = {
{0, '0'}, {1, '1'}, {2, '2'}, {3, '3'},
{4, '4'}, {5, '5'}, {6, '6'}, {7, '7'},
{8, '8'}, {9, '9'}, {10, 'A'}, {11, 'B'},
{12, 'C'}, {13, 'D'}, {14, 'E'}, {15, 'F'}
};
string toB(int num, int b) { //10进制数num转换为b进制
string ans;
while(num >= b) {
ans += mp[num % b];
num /= b;
}
if(num != 0) ans += mp[num];
reverse(ans.begin(), ans.end());
return ans;
}
AC代码1:
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;
unordered_map<int, char> mp = {
{0, '0'}, {1, '1'}, {2, '2'}, {3, '3'},
{4, '4'}, {5, '5'}, {6, '6'}, {7, '7'},
{8, '8'}, {9, '9'}, {10, 'A'}, {11, 'B'},
{12, 'C'}, {13, 'D'}, {14, 'E'}, {15, 'F'}
};
int toTen(int a, string num) //a进制数num转换为10进制
{
int ans = 0;
int n = num.size();
for (int i = 0; i < num.size(); i++) {
if (isupper(num[i]))
ans += pow(a, n - i - 1) * (num[i] - 'A' + 10);
else if (islower(num[i]))
ans += pow(a, n - i - 1) * (num[i] - 'a' + 10);
else if (isdigit(num[i]))
ans += pow(a, n - i - 1) * (num[i] - '0');
}
return ans;
}
string toB(int num, int b) { //10进制数num转换为b进制
string ans;
while(num) {
ans += mp[num % b];
num /= b;
}
reverse(ans.begin(), ans.end());
return ans;
}
int main() {
int a, b;
string n;
cin >> a >> n >> b;
int num = toTen(a, n);
cout << toB(num, b);
return 0;
}
简洁版:
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/6/30 21:26
* @Description: 3438. 数制转换 来源:北京大学考研机试题
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a, b;
string n;
cin >> a >> n >> b;
int res = 0; //a进制转换为十进制
for (auto &x: n) {
if(isupper(x)) res = res * a + x - 'A' + 10;
if(islower(x)) res = res * a + x - 'a' + 10;
if(isdigit(x)) res = res * a + x - '0';
}
string ans; //十进制转换为b进制
while (res) {
int tmp = res % b;
if(tmp >= 10)
ans += char(tmp + 'A' - 10);
else
ans += char(tmp + '0');
res /= b;
}
reverse(ans.begin(), ans.end());
cout << ans << '\n';
return 0;
}
边栏推荐
- Build your own website (23)
- Installation and configuration of network hard disk NFS
- Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
- One brush 142 monotone stack next larger element II (m)
- 【Try to Hack】主动侦查隐藏技术
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
- visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
- [2. Basics of Delphi grammar] 1 Identifiers and reserved words
- Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
- 汇编实例解析--实模式下屏幕显示
猜你喜欢
PHP online confusion encryption tutorial sharing + basically no solution
Unity notes unityxr simple to use
Depth first search of graph
静态程序分析(一)—— 大纲思维导图与内容介绍
Test your trained model
C语言按行修改文件
Network security web penetration technology
Leetcode: lucky number in matrix
Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
Swm32 series Tutorial 4 port mapping and serial port application
随机推荐
Design e-commerce spike
When absolutely positioned, the element is horizontally and vertically centered
Atom QT 16_ audiorecorder
Apache service suspended asynchronous acceptex failed
静态程序分析(一)—— 大纲思维导图与内容介绍
Meituan side: why does thread crash not cause JVM crash
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
Simple configuration of postfix server
Build your own website (23)
An example of HP array card troubleshooting
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
Assembly instance analysis -- screen display in real mode
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
The way of wisdom (unity of knowledge and action)
Leetcode: lucky number in matrix
The difference between get and post
Rsync remote synchronization
LeetCode13.罗马数字转整数(三种解法)
C language string practice
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da