Acwing124 进制转换
任意进制转换
题意:任意进制的转换
代码:https://www.acwing.com/problem/content/126/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int T;
cin >> T;
while (T -- )
{
int a, b;
string line;
cin >> a >> b >> line;
vector<int> number;
for (auto c : line)
{
if (c <= '9') number.push_back(c - '0');
else if (c <= 'Z') number.push_back(c - 'A' + 10);
else number.push_back(c - 'a' + 36);
}
reverse(number.begin(), number.end());
vector<int> res;
while (number.size())
{
int t = 0;
for (int i = number.size() - 1; i >= 0; i -- )
{
number[i] += t * a;
t = number[i] % b;
number[i] /= b;
}
res.push_back(t);
while (number.size() && !number.back()) number.pop_back();
}
reverse(res.begin(), res.end());
string b_line;
for (auto x : res)
{
if (x <= 9) b_line += char('0' + x);
else if (x <= 35) b_line += char('A' + x - 10);
else b_line += char('a' + x - 36);
}
cout << a << ' ' << line << endl;
cout << b << ' ' << b_line << endl;
cout << endl;
}
return 0;
}

查看1道真题和解析
360集团公司氛围 420人发布
