Description
Write a program to convert numbers in one base to numbers in a second base. There is different digits:
{0-9,a-z,a-z}
Hint:if 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 would have a (decimal) input base followed by a (decimal) output base followed by a number expr Essed in the input base. Both the input base and the output base is in the range from 2 to 62. That was (in decimal) A = ten, B = One, ..., z = +, A = $, B = Notoginseng, ..., z = (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 is the input base in decimal followed by a space then the input number (as given expressed in the Put base). The second output line should is the output base followed by a space then the input number (as expressed in the output bas e). The third output line is blank.
Sample Input
862 2 abcdefghiz10 123456789012345678901234567890123456789016 3a0c92075c0dbf3b8acbc5f96ce3f0ad235 23 333ymhoue8jplt7ox6k9fycq8a23 946b9aa02mi37e3d3mmj4g7bl2f0549 1vbdksimjl3jjrgadlufcawj61 5 DL9MDSWQWHJDNTOKCSWE1S5 10 42104444441001414401221302402201233340311104212022133030
Sample Output
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>using namespacestd;intMain () {intn,i,x,y,l,k,t[ -],b[ -]; Chars[ -],ans[ -];//because more than a decimal number contains letters, it is stored in a string array for(SCANF ("%d", &n); n--;) {scanf ("%d%d%s",&x,&y,s); for(I=k=strlen (s);0<i--;) T[k-1-i]=s[i]-(s[i]< -? -:s[i]< the? -: A);//Ascall codes 48 to 57 are characters 0 through one, 97 to 122 correspond to lowercase A to B, and the characters are converted into corresponding decimal numbers in the array T. for(l=0; k;) { for(I=k;1<i--;)//I really don't know what that means .{t[i-1]+=t[i]%y*x; T[i]/=y;//it seems to be to make the Y in addition to t[0]} b[l++]=t[0]%y;//because T[0] is the lowest, the equivalent of t[0] is treated as a separate unit. t[0]/=y; for(;0<k&&!t[k-1];k--);//when the t highest bit is 0 o'clock, the length of T is shortened. } for(ans[l]=i=0; i<l;i++) Ans[l-1-i]=b[i]+ (b[i]<Ten? -:b[i]< $? -: A); printf ("%d%s\n%d%s\n\n", X,s,y,ans); } return 0;
http://poj.org/problem?id=1220
Number BASE CONVERSION (binary conversion)