-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy path6.4.cpp
55 lines (50 loc) · 1.29 KB
/
6.4.cpp
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
/*
* 题目名称:进制转换2
* 题目来源:清华大学复试上机题
* 题目链接:http://t.cn/AiCuKG7E
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
char IntToChar(int x) { //数字变成字符
if (x < 10) {
return x + '0';
} else {
return x - 10 + 'a';
}
}
int CharToInt(char c) { //字符变成数字
if (c >= '0' && c <= '9') {
return c - '0';
} else {
return c - 'A' + 10;
}
}
int main() {
int m, n;
scanf("%d%d", &m, &n);
string str;
cin >> str;
long long number = 0;
for (int i = 0; i < str.size(); ++i) { //从m进制变成十进制
number *= m;
number += CharToInt(str[i]);
}
vector<char> answer;
if (number == 0) {
answer.push_back('0');
} else {
while (number != 0) { //从十进制变成n进制
answer.push_back(IntToChar(number % n));
number /= n;
}
}
for (int i = answer.size() - 1; i >= 0; --i) { //逆序输出
printf("%c", answer[i]);
}
printf("\n");
return 0;
}