描述Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:
{ 0-9,A-Z,a-z }
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number.
输入The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, …, Z = 35, a = 36, b = 37, …, z = 61 (0-9 have their usual meanings).
输出The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank.
样例输入
8 62 2 abcdefghiz 10 16 1234567890123456789012345678901234567890 16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 23 333YMHOUE8JPLT7OX6K9FYCQ8A 23 49 946B9AA02MI37E3D3MMJ4G7BL2F05 49 61 1VbDkSIMJL3JjRgAdlUfcaWj 61 5 dl9MDSWqwHjDnToKcsWE1S 5 10 42104444441001414401221302402201233340311104212022133030
样例输出
62 abcdefghiz 2 11011100000100010111110010010110011111001001100011010010001 10 1234567890123456789012345678901234567890 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 333YMHOUE8JPLT7OX6K9FYCQ8A 35 333YMHOUE8JPLT7OX6K9FYCQ8A 23 946B9AA02MI37E3D3MMJ4G7BL2F05 23 946B9AA02MI37E3D3MMJ4G7BL2F05 49 1VbDkSIMJL3JjRgAdlUfcaWj 49 1VbDkSIMJL3JjRgAdlUfcaWj 61 dl9MDSWqwHjDnToKcsWE1S 61 dl9MDSWqwHjDnToKcsWE1S 5 42104444441001414401221302402201233340311104212022133030 5 42104444441001414401221302402201233340311104212022133030 10 1234567890123456789012345678901234567890
题解:就是用字符串加短除法(就是化为10进制再短除,采用的是边递推边计算的方法)
/*
关于短除法,就是把每一位(这里指的每一位是指个位十位之类的)
除以要转换的进制的余数在乘以当前进制的值加到下一位去,
当前位的值就为商,
然后这样一直进行到最后一位(也就是个位)个位在对所须转换的进制在取模,
那么这个模就是转换后的结果。
多次重复,直到最后一位为0,从后往前看就是答案。
*/
/* 关于短除法,就是把每一位(这里指的每一位是指个位十位之类的) 除以要转换的进制的余数在乘以当前进制的值加到下一位去, 当前位的值就为商, 然后这样一直进行到最后一位(也就是个位)个位在对所须转换的进制在取模, 那么这个模就是转换后的结果。 多次重复,直到最后一位为0,从后往前看就是答案。 */ #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=1000000+10; int n,A,B,cnt; int a[N],ans[N]; char s[N]; int digit(char ch) { if (ch<='9') return ch-'0'; if (ch<='Z') return ch-'A'+10; return ch-'a'+36; } char tochar(int x) { if (x<=9) return '0'+x; if (x<=35) return 'A'+x-10; return 'a'+x-36; } void solve(int n) { bool ok=1; while (ok) { a[0]=0; ok=0; for (int i=n;i;i--) { a[i-1]+=(a[i]%B)*A; a[i]/=B; if (a[i]) ok=1; } ans[++cnt]=a[0]/A; } } int main() { int T; scanf("%d",&T); while (T--) { scanf("%d%d%s",&A,&B,s+1); for (int i=1;i<=strlen(s+1);i++) a[strlen(s+1)-i+1]=digit(s[i]); cnt=0; solve(strlen(s+1)); printf("%d %s\n%d ",A,s+1,B); for (int i=cnt;i;i--) printf("%c",tochar(ans[i])); printf("\n\n"); } return 0; }