当前位置:网站首页>AcWing 3438. Number system conversion
AcWing 3438. Number system conversion
2022-07-03 17:17:00 【How far is it forever】
Find the conversion of any two non negative integers with different bases (22 Base number ∼∼ 1616 Base number ), The integer given is in int Within the scope of .
The symbols of different bases are (0,1,…,9,a,b,…,f0,1,…,9,a,b,…,f) perhaps (0,1,…,9,A,B,…,F0,1,…,9,A,B,…,F)
Input format
There is only one line of input , Contains three integers a,n,b.a It means the following n yes a Hexadecimal integer ,b Express the desire to a Hexadecimal integer nn convert to bb Hexadecimal integer .
a,ba,b It's a decimal integer .
Data may contain leading zeros .
Output format
The output contains a line , This line has an integer for the converted bb Hexadecimal number .
When output, all alphabetic symbols are in upper case , namely (0,1,…,9,A,B,…,F0,1,…,9,A,B,…,F).
Data range
2≤a,b≤162≤a,b≤16,
A given a Hexadecimal integer n The value range in decimal system is [1,2147483647][1,2147483647].
sample input :
15 Aab3 7
sample output :
210306The main idea of the topic :
take a Hexadecimal integer n Convert to b Hexadecimal integer
Ideas :
With 10 Base centered First the a Base to zero 10 Base number Then convert the converted decimal system to b Base number
Any hexadecimal number is converted to 10 Base number
int toTen(int a, string num) //a Hexadecimal number num Convert to 10 Base number
{
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;
}Decimal to arbitrary base
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 Hexadecimal number num Convert to b Base number
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 Code 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 Hexadecimal number num Convert to 10 Base number
{
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 Hexadecimal number num Convert to b Base number
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;
}
Concise Edition :
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/6/30 21:26
* @Description: 3438. Number conversion source : Peking University postgraduate entrance examination machine test questions
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a, b;
string n;
cin >> a >> n >> b;
int res = 0; //a Conversion from decimal to decimal
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; // Decimal to b Base number
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;
}边栏推荐
- VM11289 WAService. js:2 Do not have __ e handler in component:
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
- Atom QT 16_ audiorecorder
- RDS数据库的监测页面在哪看?
- kubernetes资源对象介绍及常用命令(三)
- Rsync远程同步
- 人生还在迷茫?也许这些订阅号里有你需要的答案!
- New library online | cnopendata China bird watching record data
- Simple use of unity pen XR grab
- 定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数
猜你喜欢

Kotlin learning quick start (7) -- wonderful use of expansion

Test your trained model

建立自己的网站(23)

New library online | cnopendata complete data of Chinese insurance institution outlets

线程池:业务代码最常用也最容易犯错的组件
![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)

Atom QT 16_ audiorecorder

Analysis of variance summary

图之深度优先搜索

Unity notes unityxr simple to use
随机推荐
Deep understanding of grouping sets statements in SQL
绝对定位时元素水平垂直居中
A day's work list of an ordinary programmer
Squid 服务启动脚本
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
鸿蒙第三次培训
Luogu: p2685 [tjoi2012] Bridge
SWM32系列教程4-端口映射及串口应用
One brush 142 monotone stack next larger element II (m)
RedHat 6.2 configuring ZABBIX
September, 19, "cam principle and application" online assignment [Full Score answer]
C语言按行修改文件
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
新库上线 | CnOpenData中国保险机构网点全集数据
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
Kotlin learning quick start (7) -- wonderful use of expansion
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
Select 3 fcpx plug-ins. Come and see if you like them
On Lagrange interpolation and its application