当前位置:网站首页>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;
}边栏推荐
- Atom QT 16_ audiorecorder
- ANOVA example
- [combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
- visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
- Assembly instance analysis -- screen display in real mode
- One brush 142 monotone stack next larger element II (m)
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- SVN如何查看修改的文件记录
- [combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
- RedHat 6.2 配置 Zabbix
猜你喜欢

UE4 official charging resources, with a total price of several thousand

Simple use of unity pen XR grab

人生还在迷茫?也许这些订阅号里有你需要的答案!

【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用

手把手带你入门 API 开发

Bcvp developer community 2022 exclusive peripheral first bullet

【JokerのZYNQ7020】DDS_ Compiler。

2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)

Redis:关于列表List类型数据的操作命令

UCORE overview
随机推荐
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
New features of C 10
Take you to API development by hand
[combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Open vsftpd port under iptables firewall
27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
Apache service suspended asynchronous acceptex failed
Life is still confused? Maybe these subscription numbers have the answers you need!
[combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
绝对定位时元素水平垂直居中
简单配置PostFix服务器
Kotlin学习快速入门(7)——扩展的妙用
When absolutely positioned, the element is horizontally and vertically centered
Redis: operation commands for list type data
定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数
Thread pool: the most common and error prone component of business code
One brush 142 monotone stack next larger element II (m)