1 条题解

  • 0
    @ 2023-6-11 12:16:31

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    string num (int n)
    {
        string s[]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
        return s[n];
    }
    char fun(string a)
    {
        int k=1,s=0;
        for (int i=2;i>=0;i--)
        {
            s=s+(a[i]-48)*k;
            k=k*2;
        }
        return char(s+48);
    }
    int main()
    {
        string r,a,b;//b存放二进制 
        cin>>a;//十六进制数 
         
        for (int i=0;i<a.size();i++)
        {
            if (a[i]>='A') b=b+num(a[i]-55);
            else b=b+num(a[i]-48);
        }
        
        //删除前导0
    	while(b[0] == '0' && b.size() != 1) {
    		b.erase(0,1);
    	}
        int t=b.size();
        if (t%3!=0)
        {
            for (int i=1;i<=3-t%3;i++)
            {
                b="0"+b;
            }
        }
        
        //2进制转8进制 
        for (int i=0;i<b.size();i+=3)
        {
            r=r+fun(b.substr(i,3));
        }
        if (a!="0") cout<<r<<endl;
        else cout<<0<<endl;
        return 0;
    }
    

    Python :

    n=input()
    a=int(n,16)
    b=oct(a)[2:]
    print(b)
    
    • 1

    信息

    ID
    2210
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者