当前位置:网站首页>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;
}边栏推荐
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- C语言字符串反转
- Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
- SVN如何查看修改的文件记录
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
- 大变局!全国房价,跌破万元大关
- How to judge the region of an IP through C?
- September, 19, "cam principle and application" online assignment [Full Score answer]
- UE4 official charging resources, with a total price of several thousand
猜你喜欢
![[try to hack] active detection and concealment technology](/img/43/d48f851268fec566ce0cc83bd9557e.png)
[try to hack] active detection and concealment technology

Kubernetes resource object introduction and common commands (4)

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
![[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework](/img/df/a7719bcb00ff66e21f3a391ab94573.png)
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework

问题随记 —— 在 edge 上看视频会绿屏

New features of C 10

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

Design e-commerce spike

Fast Ethernet and Gigabit Ethernet: what's the difference?
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack
随机推荐
[RT thread] NXP rt10xx device driver framework -- pin construction and use
Redis:关于列表List类型数据的操作命令
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
One brush 144 force deduction hot question-1 sum of two numbers (E)
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
線程池:業務代碼最常用也最容易犯錯的組件
汇编实例解析--实模式下屏幕显示
On Lagrange interpolation and its application
POM in idea XML graying solution
An example of HP array card troubleshooting
Where is the database account used when running SQL tasks in data warehouse tasks configured
匯編實例解析--實模式下屏幕顯示
Talk about several methods of interface optimization
网络硬盘NFS的安装与配置
ANOVA example
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
Test your trained model
When absolutely positioned, the element is horizontally and vertically centered
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
Great changes! National housing prices fell below the 10000 yuan mark