# 数的进制转换

 1 #include <bits/stdc++.h>
2 using namespace std;
3 int main() {
4     int t;
5     cin >> t;
6     while (t--) {
7         int a, b;
8         string a_line, b_line;
9         cin >> a >> b >> a_line;
10         vector<int> number;
11         for (int i = 0; i < a_line.size(); i++) {
12             if (a_line[i] >= '0' && a_line[i] <= '9') {
13                 number.push_back(a_line[i] - '0');
14             }
15             if (a_line[i] >= 'A' && a_line[i] <= 'Z') {
16                 number.push_back(a_line[i] - 'A' + 10);
17             }
18             if (a_line[i] >= 'a' && a_line[i] <= 'z') {
19                 number.push_back(a_line[i] - 'a' + 36);
20             }
21         }
22         reverse(number.begin(), number.end());
23         vector<int> res; //答案
24
25         while (number.size()) { //只要number不是空的,也就是说只要还没有被除成0
26             int r = 0; //存储进位,上一位的余数
27             for (int i = number.size() - 1; i >= 0; i--) {
28                 number[i] += r * a;
29                 r = number[i] % b;
30                 number[i] /= b;
31             }
32             res.push_back(r); //个十百千...
33             while (number.size() && number.back() == 0) {
34                 number.pop_back();
35             }
36         }
37         reverse(res.begin(), res.end());
38         for (int i = 0; i < res.size(); i++) {
39             if (res[i] <= 9) {
40                 b_line += char(res[i] + '0');
41             }
42             if (res[i] >= 10 && res[i] <= 35) {
43                 b_line += char(res[i] + 'A' - 10);
44             }
45             if (res[i] >= 36) {
46                 b_line += char(res[i] + 'a' - 36);
47             }
48         }
49         cout << a << " " << a_line << endl;
50         cout << b << " " << b_line << endl;
51         cout << endl;
52     }
53     return 0;
54 }

posted @ 2020-11-24 20:01  kyk333  阅读(79)  评论(0编辑  收藏  举报