当前位置:网站首页>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;
}边栏推荐
- Apache服务挂起Asynchronous AcceptEx failed.
- PHP online confusion encryption tutorial sharing + basically no solution
- ucore概述
- 汇编实例解析--实模式下屏幕显示
- [try to hack] active detection and concealment technology
- 设计电商秒杀
- One brush 149 force deduction hot question-10 regular expression matching (H)
- 建立自己的网站(23)
- Necessary ability of data analysis
- Squid service startup script
猜你喜欢

线程池:业务代码最常用也最容易犯错的组件

Redis: operation commands for list type data

Kubernetes resource object introduction and common commands (III)

C语言按行修改文件

ucore概述

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

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

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos

Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing

大消费企业怎样做数字化转型?
随机推荐
聊聊接口优化的几个方法
Network security web penetration technology
BYD and great wall hybrid market "get together" again
Build your own website (23)
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Solution to long waiting time of SSH connection to remote host
27. Input 3 integers and output them in descending order. Pointer method is required.
Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
Unity notes unityxr simple to use
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
How to promote cross department project collaboration | community essay solicitation
Take you to API development by hand
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
How to allow remote connection to MySQL server on Linux system?
匯編實例解析--實模式下屏幕顯示
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
C language string practice
[RT thread] NXP rt10xx device driver framework -- pin construction and use
Golang单元测试、Mock测试以及基准测试
[try to hack] active detection and concealment technology