当前位置:网站首页>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;
}边栏推荐
- Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
- [try to hack] active detection and concealment technology
- Kotlin learning quick start (7) -- wonderful use of expansion
- Talk about several methods of interface optimization
- Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
- On Lagrange interpolation and its application
- [combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
- How to train mask r-cnn model with your own data
- 新库上线 | CnOpenData中国保险机构网点全集数据
- Fast Ethernet and Gigabit Ethernet: what's the difference?
猜你喜欢

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

Fast Ethernet and Gigabit Ethernet: what's the difference?

Kubernetes resource object introduction and common commands (4)

Why is WPA3 security of enterprise business so important?

What is your income level in the country?

Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard

Test your trained model

ANOVA example

Analysis of variance summary

Leetcode: lucky number in matrix
随机推荐
Kotlin学习快速入门(7)——扩展的妙用
Kubernetes resource object introduction and common commands (4)
Why is WPA3 security of enterprise business so important?
免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
UE4 official charging resources, with a total price of several thousand
New library online | cnopendata China bird watching record data
C语言字符串反转
On Lagrange interpolation and its application
Where is the database account used when running SQL tasks in data warehouse tasks configured
[JDBC] API parsing
静态程序分析(一)—— 大纲思维导图与内容介绍
大变局!全国房价,跌破万元大关
C语言按行修改文件
人生还在迷茫?也许这些订阅号里有你需要的答案!
New library online | cnopendata complete data of Chinese insurance institution outlets
Swm32 series Tutorial 4 port mapping and serial port application
Assembly instance analysis -- screen display in real mode
The difference between get and post
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
【JokerのZYNQ7020】DDS_ Compiler。