Pku1220 number base conversion big integer arbitrary hexadecimal conversion Template

Source: Internet
Author: User

There are T groups of cases. Each group gives you a from, to, and string S. Convert the string s from base to base.

Link: pku1220 number base Conversion

Question Analysis: directly simulate the manual method during the hexadecimal conversion until the string S is exhausted.

  # Include  <  Iostream  >  
# Include < String >
Using Namespace STD;

String Idx = " 0123456789 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz " ;
Int Getvalue ( Char Ch) // Converts character C to a corresponding number.
{
If (CH > = ' 0 ' && Ch <= ' 9 ' ) Return Ch - ' 0 ' ;
Else If (CH > = ' A ' && Ch <= ' Z ' ) Return Ch - ' A ' + 10 ;
Else Return Ch - ' A ' + 36 ;
}
// Converts a string from base to base integer.
String Change ( String S, Int From, Int To)
{
String Res = "" ;
Int R, I, G, T, Sum = 1 , Len = S. Size ();
While (Sum ! = 0 ) // Sum is used as the end mark of the loop
{
R = Sum = 0 ;
For (I = 0 ; I < Len; I ++ )
{
T = Getvalue (s [I]);
Sum + = T; // Count the numbers and
G = T + From * R;
S [I] = Idx [g / To]; // Store the operator in the base
R = G % To; // Except n
}
If (Sum > 0 )
Res = Idx. substr (r, 1 ) + Res; // The remainder obtained first is placed at a low level.
}
If (Res = "" ) // Result is an empty string indicating that the result of the conversion is 0.
Res = " 0 " ;
Return Res;
}

Int Main (){
String S;
Int I, T, from,;
CIN > T;
While (T -- ){
CIN > From > To > S;
Cout < From < " " < S < Endl;
Cout < To < " " < Change (s, from,) < Endl < Endl;
}
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.