POJ1220 (large number in conversion)

Source: Internet
Author: User

Number BASE CONVERSION
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4652 Accepted: 2132

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 directly on the template principle see: http://www.cnblogs.com/phinecos/archive/2009/09/11/1564975.html
/*id:linkarftcprog:1220.cpplang:c++*/#include<map>#include<Set>#include<cmath>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<string>#include<utility>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1e-8#defineRandin Srand ((unsigned int) time (NULL))#defineInput freopen ("Input.txt", "R", stdin)#defineDebug (s) cout << "s =" << s << endl;#defineOutstars cout << "*************" << Endl;Const DoublePI = ACOs (-1.0);Const DoubleE = exp (1.0);Const intINF =0x3f3f3f3f;Const intINF =0x7fffffff; typedefLong Longll;Const intMAXN = +;CharSTR[MAXN];//Input StringintSTART[MAXN], ANS[MAXN], RES[MAXN];//dividend, quotient, remainderintOldbase, Newbase;//conversion before and after binary//A single character gets a numberintGetnum (Charc) {//The binary characters here are first digits, then uppercase letters, and then lowercase letters    if(c >='0'&& C <='9')returnC'0';//Digital    if(c >='A'&& C <='Z')returnC'A'+Ten;//Uppercase Letters    returnC'a'+ $;}//numbers Get charactersCharGetChar (inti) {if(I >=0&& I <=9)returni +'0'; if(I >=Ten&& I <= *)returnITen+'A'; returnI $+'a';}//Restores the individual digits of the input string to a digital formvoidChange () {start[0] = strlen (str);//The array 's 0-bit memory is the array length     for(inti =1; I <= start[0]; i + +) start[i] = Getnum (str[i-1]);}voidsolve () {memset (res,0,sizeof(res));//The remaining digits are initialized to null    inty, I, J;  while(start[0] >=1) {y=0; i =1; ans[0] = start[0];  while(I <= start[0]) {y= y * oldbase +Start[i]; Ans[i+ +] = y/newbase; Y%=newbase; } res[+ + res[0]] = y;//this round gets the remainderi =1;//find the starting point for the next round of business, and remove the previous 0         while((I <= ans[0]) && (ans[i] = =0)) I + +; memset (Start,0,sizeof(start));  for(j = i; J <= ans[0]; J + +) start[++ start[0]] =Ans[j]; memset (ans,0,sizeof(ans)); }}voidOutput () {//reverse output from high to low     for(inti = res[0]; I >=1; I--) printf ("%c", GetChar (Res[i])); printf ("\ n");}intMain () {//input;    intT; scanf ("%d", &T);  while(T--) {scanf ("%d%d%s", &oldbase, &newbase, str); printf ("%d%s\n", Oldbase, str);        Change ();        Solve (); printf ("%d", newbase);        Output (); printf ("\ n"); }    return 0;}

POJ1220 (large number in conversion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.