Hex turn octal (binary relations)

 

Description

Given a positive integer n hexadecimal, octal output thereof corresponds.

Input

The first act input a 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

N output lines, each act input octal positive integer corresponding.

Sample Input

2
39
123ABC

Sample Output

71
4435274

note

Hexadecimal number entered does not have a leading zero, such as 012A.

Octal number also can not have a leading zero.

prompt

First convert a hexadecimal number to a decimal number, and then converted by a binary number to octal.

 

Problem-solving ideas:

First talk about some relevant knowledge

Binary Hex

To convert a binary number to hexadecimal, that is, to a period of four were converted to hexadecimal.

From right to left all four, for example 100,111,110,110,101, left less than four can be used to fill up 0100,1111,1011,01012 0, so the front of the Binary to hexadecimal 4FB5.

Similarly Binary octal , some are three, are converted into octal.

Hex converted into binary

Conversely, when you see FD, quickly convert it to binary method

First converted F:

See F, you need to know that it is 15, then 15 8421 How to take care of it? It should be 8 + 4 + 2 + 1, the four are all 1: 1111.

Then converted D:

See D, I know that it is 13, 13, 8421 How to take care of it? It should be: 4 + 8 + 1, namely: 1101.

Therefore, FD is converted into a binary number is: 11111101

Due to convert hex into binary fairly straightforward, so we need to convert a decimal number into a binary number, it can be converted to hexadecimal and then converted to binary.

 

For purposes of this title again, converted to hexadecimal octal, hexadecimal be first converted into binary, octal and then converted into binary.

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=1e5+10;
17 using namespace std;
18 
19 map<string,string> mp;
20 
21 int main()
22 {
23     #ifdef DEBUG
24     freopen("sample.txt","r",stdin);
25     #endif
26     
27     mp["0"]="0000"; mp["1"]="0001"; mp["2"]="0010"; mp["3"]="0011"; mp["4"]="0100";
28     mp["5"]="0101"; mp["6"]="0110"; mp["7"]="0111"; mp["8"]="1000"; mp["9"]="1001";
29     mp["A"]="1010"; mp["B"]="1011"; mp["C"]="1100"; mp["D"]="1101"; mp["E"]="1110"; mp["F"]="1111";
30     
31     mp["000"]="0"; mp["001"]="1"; mp["010"]="2"; mp["011"]="3"; 
32     mp["100"]="4"; mp["101"]="5"; mp["110"]="6"; mp["111"]="7";
33     
34     int n;
35     cin>>n;
36     string str;
37     while(n--)
38     {
39         cin>>str;        //十六进制 
40         string temp="";    //Binary memory 
41 is          String ANS = "" ;     // save octal 
42 is          for ( int I = 0 ; I <str.size (); I ++) // hexadecimal is converted into binary 
43 is              TEMP = MP + [str.substr (I , . 1 )];
 44 is          the while (temp.size ()% . 3 ) // enough digits foregoing filled 0 
45              TEMP = " 0 " + TEMP;
 46 is          int In Flag = 0 ;
 47          for ( int I = 0 ; I <= temp.size () - . 3; I + = . 3 ) // binary conversion octal 
48          {
 49              IF (In Flag == 0 ) // remove preamble 0 
50              {
 51 is                  IF (MP [temp.substr (I, . 3 )] == " 0 " ) Continue ;
 52 is                  the else In Flag = . 1 ;
 53 is              }
 54 is              ANS = MP + [temp.substr (I, . 3 )];
 55          }
 56 is          COUT ANS << << endl;
 57 is      }
 58      
59     return 0;
60 }

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12285971.html