zoukankan      html  css  js  c++  java
  • 蓝桥杯训练——基础练习 十六进制转八进制

    》》原题传送门《《

    思路:本来想偷懒直接调用,C语言自带的进制转化函数,但是交了一发之后才发现这个数太太太太大了;所以还不能先转化为十进制,当然如果你愿意用数组存的话,蛮难搞的。这个题目就是先转化成二进制用数组存,十六进制转二进制,一位转四位,二进制转八进制,三位转一位;注意前导零,注意细节不然就会像我一样发现自己傻不啦叽。

    #include<cstdio>
    #include<stack>
    #include<string>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[500005], b[500010];
    int rnum(char ch){
        if (ch >= '0'&&ch <= '9')
            return ch - '0';
        if (ch >= 'A'&&ch <= 'F')
            return ch - 'A' + 10;
    }
    int main(){
    
        
        int n; cin >> n;
        int ta[10];
        getchar();
        char cnt;
        while (n--){
            int len = 0;
            while ((cnt = getchar()) != '
    '){
                int num = rnum(cnt);
                int ans = 0;
                //cout <<"num = "<< num << endl;
                while (num > 0){
                    b[len++] = num % 2;
                    num /= 2;
                    ans++;
                }
                //cout << "ans = " << ans << endl;
                for (; ans < 4; ans++)
                    b[len++] = 0;
                for (int i = len - 4, j = 0; i < len; i++, j++){
                    ta[j] = b[i];
                }
                for (int i = len - 4, j = 3; i < len; i++, j--){
                    b[i] = ta[j];
                }
            }
            /*for (int i = 0; i < len; i++){
                cout << b[i];
                if (i % 4 == 3)cout << " ";
            }
            cout << " len = " << len << endl;
            cout << endl;*/
            int ocn = len / 3;
            if (len % 3 == 0) ocn -= 1; 
            int len_a = 0, i, j, temp;
            for (i = len - 1, j = 0; j < ocn; j++){
                temp = b[i--];
                temp += b[i--] * 2;
                temp += b[i--] * 4;
                //cout << "temp = " << temp <<" "<< i << endl;
                a[len_a++] = temp;
            }
            
            temp = 0;
            if (i >= 0)temp = b[i--];
            if (i >= 0)temp += b[i--] * 2;
            if (i >= 0)temp += b[i--] * 4;
            if (temp > 0)a[len_a++] = temp;
    
            for (int i = len_a - 1; i >= 0; i--){
                if (i == len_a - 1 && a[i] == 0)continue;
                cout << a[i];
            }
            cout << endl;
            //cout << a << endl;
            //long int num = strtol(a, &b, 16);
            //itoa(num, c, 8);
            //cout << std::oct << num << endl;
            //cout << b << endl;
        }
        
        return 0;
    }
  • 相关阅读:
    North North West
    HDU-5387 Clock
    HDU-1036 Average is not Fast Enough!
    Growling Gears
    HDU-5375 Gray code
    HDU-5373 The shortest problem
    hdu-5364 Distribution money
    UVA
    HDU-5363 Key Set
    HDU-5326 Work
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9973617.html
Copyright © 2011-2022 走看看