poj1220-任意进制转换(瞻仰下大牛代码)

来源:互联网 发布:vb 求数组最大最小值 编辑:程序博客网 时间:2024/05/20 15:11

NUMBER BASE CONVERSION
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4375 Accepted: 1969

Description

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. 

Input

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). 

Output

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. 

Sample Input

862 2 abcdefghiz10 16 123456789012345678901234567890123456789016 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 23 333YMHOUE8JPLT7OX6K9FYCQ8A23 49 946B9AA02MI37E3D3MMJ4G7BL2F0549 61 1VbDkSIMJL3JjRgAdlUfcaWj61 5 dl9MDSWqwHjDnToKcsWE1S5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz2 1101110000010001011111001001011001111100100110001101001000110 123456789012345678901234567890123456789016 3A0C92075C0DBF3B8ACBC5F96CE3F0AD216 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 333YMHOUE8JPLT7OX6K9FYCQ8A35 333YMHOUE8JPLT7OX6K9FYCQ8A23 946B9AA02MI37E3D3MMJ4G7BL2F0523 946B9AA02MI37E3D3MMJ4G7BL2F0549 1VbDkSIMJL3JjRgAdlUfcaWj49 1VbDkSIMJL3JjRgAdlUfcaWj61 dl9MDSWqwHjDnToKcsWE1S61 dl9MDSWqwHjDnToKcsWE1S5 421044444410014144012213024022012333403111042120221330305 4210444444100141440122130240220123334031110421202213303010 1234567890123456789012345678901234567890

Source

Greater New York 2002

#include<cstdio>#include<cstring>int i,l,k,a,b,T,t[555],A[555];char s[555],d[555];main(){for(scanf("%d",&T);T--;){scanf("%d%d%s",&a,&b,s);for(k=i=strlen(s);i>=0;i--)t[k-1-i]=s[i]-(s[i]<58?48:s[i]<97?55:61);for(l=0;k;){for(i=k;1<i--;){t[i-1]+=t[i]%b*a;t[i]/=b;}A[l++]=t[0]%b;t[0]/=b;for(;0<k&&!t[k-1];k--);}for(d[l]=i=0;i<l;i++)d[l-1-i]=A[i]+(A[i]<10?48:A[i]<36?55:61);printf("%d %s\n%d %s\n\n",a,s,b,d);}}


0 0
原创粉丝点击