AcWing 124. 数的进制转换
原题链接
简单
作者:
彩
,
2024-04-12 15:48:01
,
所有人可见
,
阅读 3
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n;
int main()
{
cin >> n;
int a, b;
string x;
while(n--)
{
cin >> a >> b >> x;
vector<int> ax;
for(auto t : x)
{
if(t >= '0' && t <= '9') ax.push_back(t - '0');
if(t >= 'A' && t <= 'Z') ax.push_back(t - 'A' + 10);
if(t >= 'a' && t <= 'z') ax.push_back(t - 'a' + 36);
}
reverse(ax.begin(), ax.end());
vector<int> res;
while(ax.size())
{
int r = 0;
for(int i = ax.size() - 1; i >= 0; i--)
{
ax[i] += r * a;
r = ax[i] % b;
ax[i] /= b;
}
res.push_back(r);
while(ax.size() && ax.back() == 0) ax.pop_back();
}
reverse(res.begin(), res.end());
string y;
for(auto t : res)
{
if(t >= 0 && t <= 9) y += char(t + '0');
if(t >= 10 && t <= 35) y += char(t - 10 + 'A');
if(t >= 36 && t <= 61) y += char(t - 36 + 'a');
}
cout << a << " " << x << endl;
cout << b << " " << y << endl;
cout << endl;
}
return 0;
}