当前位置:网站首页>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;
}边栏推荐
- How to judge the region of an IP through C?
- New library online | cnopendata complete data of Chinese insurance institution outlets
- 大变局!全国房价,跌破万元大关
- One brush 149 force deduction hot question-10 regular expression matching (H)
- One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
- Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
- Leetcode: lucky number in matrix
- 一位普通程序员一天工作清单
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- 新库上线 | CnOpenData中国观鸟记录数据
猜你喜欢

Applet setting multi account debugging

Play with fancy special effects. This AE super kit is for you

29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)

What is your income level in the country?

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

设计电商秒杀

Atom QT 16_ audiorecorder

【Try to Hack】主动侦查隐藏技术

Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack
随机推荐
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
设计电商秒杀
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
How SVN views modified file records
Design e-commerce spike
[JDBC] API parsing
免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
Analysis of variance summary
One brush 149 force deduction hot question-10 regular expression matching (H)
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
[combinatorial mathematics] recursive equation (example of recursive equation 2 Hanoi Tower | example of recursive equation 3 insertion sequencing)
Kotlin learning quick start (7) -- wonderful use of expansion
Redis:关于列表List类型数据的操作命令
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
One brush 145 force deduction hot question-2 sum of two numbers (m)
[combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
问题随记 —— 在 edge 上看视频会绿屏
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
新库上线 | CnOpenData中国观鸟记录数据
Test your trained model