题目:
解题思路:
N转M进制需要有一个中间转换,就是转十进制
题解:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1000;
int a[N];//写一个数组接s的数据
char ch[] = {'0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'A', 'B', 'C','D', 'E', 'F'};//十六进制的0~15
void solve()
{
int n, m;cin >> n >> m;
string s; cin >> s;
int len = s.length();
s = "#" + s;//将s[0]为#,方便当从一开始遍历的操着
for(int i = 1; i <= len; i++)//将字符转换为得到n进制的数字
{
if('0' <= s[i] && s[i] <= '9')a[i] = s[i] - '0';
else a[i] = s[i] - 'A' + 10;
}
ll x = 0;
for(int i = 1; i <= len; i++)x = x * n + a[i];//k进制转10进制
string ans;//将10进制转k进制模板
while(x)
{
ans += ch[x % m];
x /= m;
}
reverse(ans.begin(), ans.end());
cout << ans<< '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _;cin >> _;
while(_--)solve();//#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1000;
int a[N];//写一个数组接s的数据
char ch[] = {'0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'A', 'B', 'C','D', 'E', 'F'};//十六进制的0~15
void solve()
{
int n, m;cin >> n >> m;
string s; cin >> s;
int len = s.length();
s = "#" + s;//将s[0]为#,方便当从一开始遍历的操着
for(int i = 1; i <= len; i++)//将字符转换为得到n进制的数字
{
if('0' <= s[i] && s[i] <= '9')a[i] = s[i] - '0';
else a[i] = s[i] - 'A' + 10;
}
ll x = 0;
for(int i = 1; i <= len; i++)x = x * n + a[i];//k进制转10进制
string ans;//将10进制转k进制模板
while(x)
{
ans += ch[x % m];
x /= m;
}
reverse(ans.begin(), ans.end());
cout << ans<< '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _;cin >> _;
while(_--)solve();//N转M进制需要有一个中间转换,就是转十进制
return 0;
}
return 0;
}