Problem description
given positive integer n hexadecimal, octal output thereof corresponds.
Input format
input acts a first positive integer n (1 <= n <= 10).
Next n lines, each line of a character string from 0 to 9, capital letters A ~ F composition indicates hexadecimal to convert a positive integer, each hexadecimal number length does not exceed 100,000.
Output format
output n lines, each act input corresponding octal positive integer.
[Note ]
enter the hexadecimal number does not have leading 0, such as 012A.
Octal number also can not have a leading zero.
Sample input
2
39
123ABC
sample output
71 is
4,435,274
#include<stdio.h> #include<string> #include<iostream> #include<string.h> using namespace std; char a[16][5] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; int main(){ int n; char x[100000]; scanf("%d",&n); while(n--){ scanf("%s",x); String Y; int LON = strlen (X); for ( int I = 0 ; I <LON; I ++ ) { int NUM; // front to back stitching binary string constructed in accordance with a hexadecimal character string IF (X [I] > = 65 && X [I] <= 70 ) { // If the hexadecimal ABCDEF NUM = X [I] - 55 ; y.append [NUM] (A); } the else { // hexadecimal digital NUM = X [I] - 48 ; y.append ([NUM] a); } } Int MK; int lon2 = y.size (); IF (Y [ 0 ] == ' 0 ' ) { // If the first character string is a binary '0' for ( int I = . 1 ; I <lon2; ++ I ) IF ! (Y [I] = ' 0 ' ) { MK = I; BREAK ; } // binary string header several consecutive '0' // move forward in accordance with the string number of bits '0' for ( int I = 0 ; I <lon2-MK; I ++ ) Y [I] = Y [I + MK]; // string last invalid due to movement of a position remove y.erase (lon2- MK, MK); } // Because the string may move forward, it must be re-acquired length of the string lon2 = y.size (); int J = 0 ; int All = 0 ; int CC = . 1 ; string resu; // last octal string // changes from a binary octal, binary string is from the back // every three less than a set {3, the front fill character '0'], is converted into a decimal character, the splice configuration corresponding //To simplify '0' character complement, binary string directly from the back, with each group of three, each according to one hundred, ten, bit summation // not three, since complement is '0', so the direct disregard this operation, the summing line char KK [ 2 ]; for ( int I = lon2- . 1 ; I> = 0 ; i-- ) { int Y = NUM [I] - 48 ; // the character '1' '0' to 10, conveniently summation All NUM = + * CC; CC = CC * 10 ; // changing hundred, ten, bit J ++ ; IF (J == . 3 || I == 0 ) { // J ==. 3 is satisfied with "a set of three" IF (== All . 1 ) // I == 0 is satisfied "reaches the end of the binary string, fill '0' operations not disregard the case of a group of three" KK [ 0 ] = ' . 1 ' ; IF (All = = 10 ) KK [ 0 ] = ' 2 ' ; IF (== All . 11 ) KK [ 0 ] = ' . 3 ' ; IF (All == 100 ) KK [ 0 ] = ' . 4 '; IF (All == 101 ) KK [ 0 ] = ' . 5 ' ; IF (All == 110 ) KK [ 0 ] = ' . 6 ' ; // char KK [2] of the object, append () operation only for characters string, the length of a string array to get 2 IF (All == 111 ) // the first character is placed, the second discharge end of the string '\ 0' KK [ 0 ] = ' . 7 ' ; IF ( == All 0 ) KK [0 ] =' 0 ' ; // digital sum, matches the corresponding octal character resu.append (KK); // splicing constructs All = 0 ; CC = . 1 ; J = 0 ; } } int lon3 = resu.size (); for ( int I = lon3 - . 1 ; I> = 0 ; i--) // inverted output ,, because the binary string is scanned from the back, and append () is added to the end of the printf ( " C% " , resu [I]); COUT <<endl; } return 0; }