Poj 1220 number base conversion (high-precision hexadecimal conversion)

Source: Internet
Author: User
Tags uppercase letter
Number base Conversion
Time limit:1000 ms Memory limit:10000 K
Total submissions:3231 Accepted:1394

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 shoshould 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 shocould consist of three lines of output for each base conversion saved med. the first line shoshould 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 shocould 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 listen 23 listen 49 listen 61 1vbdksimjl3jjrgadlufcawj61 5 dl9mdswqwhjdntokcswe1s5 10 42104444441001414401221302402201233340311104212022133030

Sample output

 
62 abcdefghiz2 1101110000010001011111001001011001111100100110001101001000110 123456789012345678901234567890123456789016 20171000000000000000000001vbdksimjl3jrgadlufcawj49 jwdl9mdswqwhjdntokcswe1s61 dl9mdswqwhjdntokcswe1s5 421044444410014144012213024022012333403111042120221330305 4210444444100141440122130240220123334031110421202213303010 1234567890123456789012345678901234567890

Source

For more information about Greater New York 2002, see http://www.cnblogs.com/kuangbin/archive/2011/08/09/2132467.html.
 /*  
High-Precision hexadecimal conversion
Convert the oldbase base number into the newbase number output.
Call method, enter STR, oldbase newbase.
Change ();
Solve ();
Output ();
You can also modify output () to meet the requirements, or store it in another character array.
*/
# Include < Stdio. h >
# Include < String . H >
# Define Maxsize 1000
Char STR [maxsize]; // Input string
Int Start [maxsize], ANS [maxsize], Res [maxsize]; // Divisor, quotient, remainder
Int Oldbase, newbase; // Before and after conversion

// Returns a number for a single character.
Int Getnum ( Char C) // Here, the hexadecimal character is a digit, followed by an uppercase letter, followed by a lowercase letter
{
If (C > = ' 0 ' && C <= ' 9 ' ) Return C - ' 0 ' ; // Number
If (C > = ' A ' && C <= ' Z ' ) Return C - ' A ' + 10 ; // Uppercase letters
Return C - ' A ' + 36 ; // Lowercase letters
}
// Number to get characters
Char Getchar ( Int I)
{
If (I > = 0 && I <= 9 ) Return I + ' 0 ' ;
If (I > = 10 && I <= 35 ) Return I - 10 + ' A ' ;
Return I - 36 + ' A ' ;
}
Void Change () // Returns the digits of the input string to a number.
{
Int I;
Start [ 0 ] = Strlen (STR ); // The array 0-bit stores the array length.
For (I = 1 ; I <= Start [ 0 ]; I ++ )
Start [I] = Getnum (STR [I - 1 ]);
}
Void Solve ()
{
Memset (Res, 0 , Sizeof (RES )); // The remainder digit is blank during initialization.
Int Y, 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; // The remainder of this round
I = 1 ; // Find the starting point of the next round of commerce 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 ));
}
}
Void Output () // Output in reverse order from high to low
{
Int I;
Printf ( " % D % s \ n " , Oldbase, STR );
Printf ( " % D " , Newbase );
For (I = Res [ 0 ]; I > = 1 ; I -- )
Printf ( " % C " , Getchar (RES [I]);
Printf ( " \ N " );
}
Int Main ()
{
// Freopen ("test. In", "r", stdin );
// Freopen ("test. Out", "W", stdout );
Int T;
Scanf ( " % D " , & T );
While (T -- )
{
Scanf ( " % D % s " , & Oldbase, & Newbase, STR );
Change ();
Solve ();
Output ();
}
Return 0 ;
}

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.