3438. 数制转换
摘要
Title: 3438. 数制转换
Tag: 进制转换
Memory Limit: 64 MB
Time Limit: 1000 ms
Powered by:NEFU AB-IN
3438. 数制转换
-
题意
求任意两个不同进制非负整数的转换(2 进制 ∼ 16 进制),所给整数在 int 范围内。
不同进制的表示符号为(0,1,…,9,a,b,…,f)或者(0,1,…,9,A,B,…,F) -
思路
手写两个函数,分别是 整数转字符,字符转整数
其余正常写就行 -
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61/*
* @Author: NEFU AB-IN
* @Date: 2022-08-08 00:33:59
* @FilePath: \Acwing\3438\test.cpp
* @LastEditTime: 2022-08-09 20:29:03
*/
using namespace std;
typedef pair<int, int> PII;
const int INF = INT_MAX;
const int N = 1e6 + 10;
char itoc(int x)
{
if (x < 10)
return '0' + x;
return 'A' + x - 10;
}
int ctoi(char c)
{
if (c <= '9')
return c - '0';
if (c <= 'Z')
return c - 'A' + 10;
return c - 'a' + 10;
}
signed main()
{
IOS;
string n;
int a, b;
cin >> a >> n >> b;
// a进制转10进制
int ans = 0;
for (auto i : n)
{
ans = ans * a + ctoi(i);
}
// 10进制转b进制
string res;
while (ans)
{
res += itoc(ans % b);
ans /= b;
}
reverse(res.begin(), res.end());
cout << res << '\n';
return 0;
}