蓝桥杯 16进制转化八进制+使用二进制

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值