AcWing 124. 数的进制转换
原题链接
简单
作者:
MyPower
,
2023-04-07 16:08:14
,
所有人可见
,
阅读 135
#include <iostream>
#include <cstring>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t --)
{
int a, b;
string a_line, b_line;
cin >> a >> b >> a_line;
vector <int> res, number;
for(auto c : a_line)
{
if(c >= '0' && c <= '9') number.push_back(c - '0');
if(c >= 'A' && c <= 'Z') number.push_back(c - 'A' + 10);
if(c >= 'a' && c <= 'z') number.push_back(c - 'a' + 36);
}
reverse(number.begin(), number.end());
while(number.size())
{
int r = 0;
for(int i = number.size() - 1; i >= 0; i --)
{
//短除法 3转2
number[i] += r * a;
r = number[i] % b;
number[i] /= b;
}
res.push_back(r);
//删除前导9
while(number.size() && number.back() == 0) number.pop_back();
}
reverse(res.begin(), res.end());
for(auto x : res)
{
if(x >= 0 && x <= 9) b_line += char(x + '0');
if(x >= 10 && x <= 35) b_line += char(x - 10 + 'A');
if(x >= 36 && x <= 61) b_line += char(x - 36 + 'a');
}
cout << a << " " << a_line << endl;
cout << b << " " << b_line << endl << endl;
}
}