当前位置:网站首页>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;
}边栏推荐
- Necessary ability of data analysis
- When absolutely positioned, the element is horizontally and vertically centered
- Host based intrusion system IDS
- C语言字符串练习
- HP 阵列卡排障一例
- 匯編實例解析--實模式下屏幕顯示
- Kubernetes resource object introduction and common commands (4)
- UE4 official charging resources, with a total price of several thousand
- LeetCode 1658. Minimum operand to reduce x to 0
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
猜你喜欢

Swm32 series Tutorial 4 port mapping and serial port application

The largest matrix (H) in a brush 143 monotone stack 84 histogram

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)

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

Test your trained model

跨境电商:外贸企业做海外社媒营销的优势

Meituan side: why does thread crash not cause JVM crash

kubernetes资源对象介绍及常用命令(三)
![Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)](/img/be/4ef38f711e7319a2cc83db2bee3a07.jpg)
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)

人生还在迷茫?也许这些订阅号里有你需要的答案!
随机推荐
SWM32系列教程4-端口映射及串口应用
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
匯編實例解析--實模式下屏幕顯示
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
One brush 145 force deduction hot question-2 sum of two numbers (m)
Kubernetes resource object introduction and common commands (III)
The way of wisdom (unity of knowledge and action)
RF analyze demo build step by step
C语言按行修改文件
Analysis of variance summary
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
What is your income level in the country?
[2. Basics of Delphi grammar] 2 Object Pascal data type
聊聊接口优化的几个方法
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
Build your own website (23)
BYD and great wall hybrid market "get together" again
C语言字符串反转