#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
#include<string.h>
#include<algorithm>
using namespace std;
int getDigit(char c)
{
if(c=='0') return 0;
else if(c=='1') return 1;
else if(c=='2') return 2;
else if(c=='3') return 3;
else if(c=='4') return 4;
else if(c=='5') return 5;
else if(c=='6') return 6;
else if(c=='7') return 7;
else if(c=='8') return 8;
else if(c=='9') return 9;
else if(c=='A') return 10;
else if(c=='B') return 11;
else if(c=='C') return 12;
else if(c=='D') return 13;
else if(c=='E') return 14;
else if(c=='F') return 15;
return -1;
}
int p[100010];//存放十六进制
int q[400020];//存放二进制
int g[3]={1,2,4};
int main()
{
string s;
int n=0,length=0,i=0,j=0;
cin>>n;
while(n--)
{
cin>>s;
length=s.length();
for(i=0;i<length;i++)
{
p[i]=getDigit(s[i]);
}
for(i=0;i<length;i++)
{
for(j=3;j>=0;j--)
{
q[4*i+j]=p[i]%2;
p[i]=p[i]/2;
}
}
if((4*length)%3==0)
{
int flag=0;
for(i=0;i<4*length;i+=3)
{
int temp=0;
for(int j=2;j>=0;j--)
{
temp+=q[i+2-j]*g[j];
}
if(flag==0&&temp)//解决万一首部出现0的情况
{
cout<<temp;
flag=1;
}
else if(flag==1)
cout<<temp;
}
cout<<endl;
}
else if((4*length)%3==1)
{
int flag=0;
if(q[0])
{
cout<<q[0];
flag=1;
}
for(i=1;i<4*length;i+=3)
{
int temp=0;
for(int j=2;j>=0;j--)
{
temp+=q[i+2-j]*g[j];
}
if(flag==0&&temp)//解决万一首部出现0的情况
{
cout<<temp;
flag=1;
}
else if(flag==1)
cout<<temp;
}
cout<<endl;
}
else if((4*length)%3==2)
{
int flag=0;
if(q[0]*2+q[1])
{
cout<<q[0]*2+q[1];
flag=1;
}
for(i=2;i<4*length;i+=3)
{
int temp=0;
for(int j=2;j>=0;j--)
{
temp+=q[i+2-j]*g[j];
}
if(flag==0&&temp)//解决万一首部出现0的情况
{
cout<<temp;
flag=1;
}
else if(flag==1)
cout<<temp;
}
cout<<endl;
}
}
return 0;
}
蓝桥杯 16进制转化八进制+使用二进制
最新推荐文章于 2022-11-08 19:38:20 发布