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 ;
}