Blue Bridge Cup Practice System Submission Link
Exam questions basic practice hexadecimal to octal
Problem description
Given n hexadecimal positive integers, output their corresponding octal numbers.
Input format
The first line of input is a positive integer n (1<=n<=10).
In the next n lines, each line contains a string consisting of 0 9 and uppercase letters A F, which represents 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 input corresponding octal positive integer.
[Note] The
entered hexadecimal number will not have leading 0, such as 012A.
The output octal number cannot have a leading 0 either.
Sample input
2
39
123ABC
Sample output
71
4435274
[Prompt]
First convert the hexadecimal number into a certain hexadecimal number, and then convert the certain hexadecimal number into an octal number.
Problem-solving ideas:
Idea : Convert hexadecimal to binary, and then from binary to octal
Rule:
Hexadecimal to binary: 1 hexadecimal to 4 binary
binary to octal: 3 binary to 1 octal
Example:
hexadecimal number 12
hexadecimal to binary 0001 0010
two to eight 0 00 010 010
(counting three digits from right to left, not enough three digits to be filled with 0)
Result: 22 (leading 0 is not output)
Code C version:
//该题不能用strcat函数,会超时的
#include<stdio.h>
#include<string.h>
#define N 100010
char p[16][5]={"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"};//代表[0,15];
char s[N];//输入的16进制
char a[N*4]; //转化后的二进制
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(a,'\0',sizeof(a));
a[0]='$';
a[1]='$';
//a[2]='\0';
int cnt=2;
int i,j,k,m;
scanf("%s",s);
int ls=strlen(s);
for(i=0;i<ls;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
m=s[i]-48;
for(j=0;j<4;j++)
a[cnt++]=p[m][j];
//a[cnt]='\0';
}
if(s[i]>='A'&&s[i]<='F')
{
m=s[i]-55;
for(j=0;j<4;j++)
a[cnt++]=p[m][j];
//a[cnt]='\0';
}
}
//二进制的真实长度是cnt-2
if((cnt-2)%3==0)
k=2;
if((cnt-2)%3==1)
{
k=0;
a[0]='0',a[1]='0';//补两个0
}
if((cnt-2)%3==2)
{
k=1;
a[1]='0';
}
int flag=0,y;
for(;k<cnt;k+=3)
{
y=4*(a[k]-'0')+2*(a[k+1]-'0')+(a[k+2]-'0');//3位2进制化为1为8进制
if(y!=0)
flag=1;
if(flag==1)
printf("%d",y);
}
printf("\n");
}
return 0;
}
Code C++ version:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int i,j,n,num,len,p;
string s1,s2;
cin>>n;
while(n--)
{
cin>>s1;
s2="";
for (i=0; i<s1.length(); i++)
{
switch(s1[i])
{
case '0':
s2+="0000";
break;
case '1':
s2+="0001";
break;
case '2':
s2+="0010";
break;
case '3':
s2+="0011";
break;
case '4':
s2+="0100";
break;
case '5':
s2+="0101";
break;
case '6':
s2+="0110";
break;
case '7':
s2+="0111";
break;
case '8':
s2+="1000";
break;
case '9':
s2+="1001";
break;
case 'A':
s2+="1010";
break;
case 'B':
s2+="1011";
break;
case 'C':
s2+="1100";
break;
case 'D':
s2+="1101";
break;
case 'E':
s2+="1110";
break;
case 'F':
s2+="1111";
break;
}
}
len=s2.length();
if (len%3==1)//三个二进制代表一个八进制
s2="00"+s2;
else if (len%3==2)
s2="0"+s2;
int flag=0;
for (i=0; i<=s2.length()-3; i+=3)
{
int num=4*(s2[i]-'0')+2*(s2[i+1]-'0')+1*(s2[i+2]-'0');
//二进制每三位算出一个8进制数
if (num)
flag=1;//防止前面三个前导零?
if (flag)
cout<<num;
}
cout<<endl;
}
return 0;
}