当前位置:网站首页>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;
}边栏推荐
- Applet setting multi account debugging
- 【Try to Hack】主动侦查隐藏技术
- Host based intrusion system IDS
- RedHat 6.2 配置 Zabbix
- Kotlin learning quick start (7) -- wonderful use of expansion
- Redis: operation commands for list type data
- New features of C 10
- kubernetes资源对象介绍及常用命令(四)
- 鸿蒙第三次培训
- [2. Basics of Delphi grammar] 1 Identifiers and reserved words
猜你喜欢

How do large consumer enterprises make digital transformation?

Thread pool: the most common and error prone component of business code

C language modifies files by line

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

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

kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)

Applet setting multi account debugging

聊聊接口优化的几个方法

Kubernetes resource object introduction and common commands (III)
随机推荐
匯編實例解析--實模式下屏幕顯示
Redis: operation commands for list type data
基于主机的入侵系统IDS
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
C语言字符串练习
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
Vs code plug-in korofileheader
RDS数据库的监测页面在哪看?
What is your income level in the country?
[UE4] brush Arctic pack high quality Arctic terrain pack
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
Kotlin learning quick start (7) -- wonderful use of expansion
聊聊接口优化的几个方法
27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
One brush 149 force deduction hot question-10 regular expression matching (H)
New library online | cnopendata China bird watching record data
How to allow remote connection to MySQL server on Linux system?
定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数
Assembly instance analysis -- screen display in real mode
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]