Basic exercises of test questions hexadecimal to octal
resource constraints
Memory limit: 512.0MB C/C++ time limit: 1.0s Java time limit: 3.0s Python time limit: 5.0s
Problem Description
Given n positive hexadecimal integers, output their corresponding octal numbers.
Input format
The first line of input is a positive integer n (1<=n<=10).
Next n lines, each line is a string consisting of 0~9 and uppercase letters A~F, indicating the hexadecimal positive integer to be converted, and the length of each hexadecimal number does not exceed 100000.
Output format
Output n lines, each line corresponds to the octal positive integer input.
[Note ]
The input hexadecimal number will not have a leading 0, such as 012A.
The output octal number must also not have leading zeros.
Sample input
2
39
123ABC
Sample output
71
4435274
[ Prompt ]
First convert the hexadecimal number to a certain base number, and then convert the certain base number to an octal number.
The specific idea gives a conversion function, first convert the hexadecimal number into an integer, and then convert the integer into an octal string
Just look at the change function
Version 1: Using numbers as an intermediate process, too much data long long cannot be received at all.
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
string change(string& s)
{
string res;
long long num = 0;
int step = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
switch (s[i])
{
case '1':
num += 1 * pow(16, step++);
break;
case '2':
num += 2 * pow(16, step++);
break;
case '3':
num += 3 * pow(16, step++);
break;
case '4':
num += 4 * pow(16, step++);
break;
case '5':
num += 5 * pow(16, step++);
break;
case '6':
num += 6 * pow(16, step++);
break;
case '7':
num += 7 * pow(16, step++);
break;
case '8':
num += 8 * pow(16, step++);
break;
case '9':
num += 9 * pow(16, step++);
break;
case 'A':
num += 10 * pow(16, step++);
break;
case 'B':
num += 11 * pow(16, step++);
break;
case 'C':
num += 12 * pow(16, step++);
break;
case 'D':
num += 13 * pow(16, step++);
break;
case 'E':
num += 14 * pow(16, step++);
break;
case 'F':
num += 15 * pow(16, step++);
break;
}
}
int m;
while (num)
{
m = num % 8;
num /= 8;
res += '0'+m;
// res += to_string(m);
}
reverse(res.begin(), res.end());
return res;
}
int main()
{
int n;
cin >> n;
vector<string> v(n);
vector<string> res(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
string temp = change(v[i]);
res[i] = temp;
}
for (int i = 0; i < n; i++)
{
cout << res[i];
cout << endl;
}
return 0;
}
[Solution 2] Use 16-"2-"8 base
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
{
string tow;
string sixteen;
string eight;
cin>>sixteen;
for(int j=0; j<sixteen.length(); j++)
{
switch(sixteen[j])
{
case '0':tow+="0000";break;
case '1':tow+="0001";break;
case '2':tow+="0010";break;
case '3':tow+="0011";break;
case '4':tow+="0100";break;
case '5':tow+="0101";break;
case '6':tow+="0110";break;
case '7':tow+="0111";break;
case '8':tow+="1000";break;
case '9':tow+="1001";break;
case 'A':
case 'a':tow+="1010";break;
case 'B':
case 'b':tow+="1011";break;
case 'C':
case 'c':tow+="1100";break;
case 'D':
case 'd':tow+="1101";break;
case 'E':
case 'e':tow+="1110";break;
case 'F':
case 'f':tow+="1111";break;
}
}
if(tow.length()%3==1)
tow="00"+tow;
if(tow.length()%3==2)
tow="0"+tow;
if(!(tow[0]=='0'&&tow[1]=='0'&&tow[2]=='0'))
{
char temp;
temp = (tow[0]-'0')*4+(tow[1]-'0')*2+tow[2];
eight += temp;
}
for(int j=3; j<tow.length(); j+=3)
{
eight+=(tow[j]-'0')*4+(tow[j+1]-'0')*2+tow[j+2];
}
cout<<eight<<endl;
}
return 0;
}