【例题】lanqiao1230 进制转换
输入样例
2
2 10
10101
11 2
1793A5068
输出样例
21
10101111001010100111010101011
解题思路
先把字符串s转化成十进制,再把十进制转化成m进制。
代码
# n-->m
T=int(input())
# input:s char
# output:int
int_to_char="0123456789ABCDEF"
char_to_int={}
for i in range(16):char_to_int[int_to_char[i]]=i
def n_to_ten(n,s):ans=0l=len(s)for index, char in enumerate(s):ans+=char_to_int[char]*(n**(l-index-1))return ans
def ten_to_m(m,t):ans=[]t=int(t)while t:ans.append(t%m)t//=mans.reverse()return "".join([int_to_char[i] for i in ans])
for _ in range(T):n,m=map(int,input().split())s = input().strip()print(ten_to_m(m,n_to_ten(n,s)))