问题描述
给定n个十六进制正整数,输出它们对应的八进制数。
输入格式
输入的第一行为一个正整数n (1<=n<=10)。
接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出格式
输出n行,每行为输入对应的八进制正整数。
【注意】
输入的十六进制数不会有前导0,比如012A。
输出的八进制数也不能有前导0。
样例输入
2
39
123ABC
样例输出
71
4435274
【提示】
先将十六进制数转换成某进制数,再由某进制数转换成八进制。
解题思路:由输入的格式长度决定无法通过整数型变量进行运算,而提示给出先转换进制,首先想到十六进制和八进制对于二进制都有很方便的计算方法,由此为思路解决。
1 #include<cstdio>
2 #include<string>
3 #include<vector>
4 using namespace std;
5
6 int main(void)
7 {
8 int n;
9 char c;
10 scanf("%d", &n);
11 for(int i = 0; i < n; i++){
12 vector<char> s1;
13 int count = 0;
14 while(scanf("%c", &c) == 1 && c == '\n');
15 while(c != '\n'){
16 s1.push_back(c);
17 count++;
18 scanf("%c", &c);
19 }
20
21 if(s1[0] == '0'){
22 printf("0\n");
23 continue;
24 }
25 vector<char>::iterator it;
26 vector<char> s2;
27 for(it = s1.begin(); it != s1.end(); it++){
28 switch(*it){
29 case '0':{
30 s2.push_back('0');s2.push_back('0');
31 s2.push_back('0');s2.push_back('0');
32 break;
33 }
34 case '1':{
35 s2.push_back('0');s2.push_back('0');
36 s2.push_back('0');s2.push_back('1');
37 break;
38 }
39 case '2':{
40 s2.push_back('0');s2.push_back('0');
41 s2.push_back('1');s2.push_back('0');
42 break;
43 }
44 case '3':{
45 s2.push_back('0');s2.push_back('0');
46 s2.push_back('1');s2.push_back('1');
47 break;
48 }
49 case '4':{
50 s2.push_back('0');s2.push_back('1');
51 s2.push_back('0');s2.push_back('0');
52 break;
53 }
54 case '5':{
55 s2.push_back('0');s2.push_back('1');
56 s2.push_back('0');s2.push_back('1');
57 break;
58 }
59 case '6':{
60 s2.push_back('0');s2.push_back('1');
61 s2.push_back('1');s2.push_back('0');
62 break;
63 }
64 case '7':{
65 s2.push_back('0');s2.push_back('1');
66 s2.push_back('1');s2.push_back('1');
67 break;
68 }
69 case '8':{
70 s2.push_back('1');s2.push_back('0');
71 s2.push_back('0');s2.push_back('0');
72 break;
73 }
74 case '9':{
75 s2.push_back('1');s2.push_back('0');
76 s2.push_back('0');s2.push_back('1');
77 break;
78 }
79 case 'A':{
80 s2.push_back('1');s2.push_back('0');
81 s2.push_back('1');s2.push_back('0');
82 break;
83 }
84 case 'B':{
85 s2.push_back('1');s2.push_back('0');
86 s2.push_back('1');s2.push_back('1');
87 break;
88 }
89 case 'C':{
90 s2.push_back('1');s2.push_back('1');
91 s2.push_back('0');s2.push_back('0');
92 break;
93 }
94 case 'D':{
95 s2.push_back('1');s2.push_back('1');
96 s2.push_back('0');s2.push_back('1');
97 break;
98 }
99 case 'E':{
100 s2.push_back('1');s2.push_back('1');
101 s2.push_back('1');s2.push_back('0');
102 break;
103 }
104 case 'F':{
105 s2.push_back('1');s2.push_back('1');
106 s2.push_back('1');s2.push_back('1');
107 break;
108 }
109 }
110 }
111
112 if((count * 4) % 3 == 1){
113 s2.insert(s2.begin(), '0');s2.insert(s2.begin(), '0');
114 }
115 else if((count * 4) % 3 == 2){
116 s2.insert(s2.begin(), '0');
117 }
118
119 int first = 1;
120 for(it = s2.begin(); it != s2.end(); it += 3){
121 int re = (*it - '0') * 4 + (*(it + 1) - '0') * 2 + (*(it + 2) - '0');
122 if(first == 1){
123 if(re == 0) continue;
124 else{
125 printf("%d", re);
126 first = 0;
127 }
128 }
129 else printf("%d", re);
130 }
131 printf("\n");
132 }
133
134
135 return 0;
136 }
来源:oschina
链接:https://my.oschina.net/u/4412369/blog/4243200