当前位置:网站首页>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;
}
边栏推荐
- Great changes! National housing prices fell below the 10000 yuan mark
- Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
- PS screen printing brush 131, many illustrators have followed suit
- Take you to API development by hand
- 大变局!全国房价,跌破万元大关
- 2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
- Simple configuration of postfix server
- One brush 149 force deduction hot question-10 regular expression matching (H)
- kubernetes资源对象介绍及常用命令(四)
猜你喜欢
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
kubernetes资源对象介绍及常用命令(四)
Thread pool: the most common and error prone component of business code
Design e-commerce spike
Build your own website (23)
Static program analysis (I) -- Outline mind map and content introduction
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
Golang单元测试、Mock测试以及基准测试
Take you to API development by hand
Talk about several methods of interface optimization
随机推荐
Kubernetes resource object introduction and common commands (III)
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
How to judge the region of an IP through C?
Unity notes unityxr simple to use
Meituan side: why does thread crash not cause JVM crash
How to allow remote connection to MySQL server on Linux system?
SVN完全备份svnadmin hotcopy
[combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)
One brush 148 force deduction hot question-5 longest palindrome substring (m)
【Try to Hack】主动侦查隐藏技术
Static program analysis (I) -- Outline mind map and content introduction
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
Select 3 fcpx plug-ins. Come and see if you like them
[UE4] brush Arctic pack high quality Arctic terrain pack
图之深度优先搜索
Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022