• 十六进制转八进制 蓝桥杯


    Description

    给定n个十六进制正整数,输出它们对应的八进制数。

    Input

    输入的第一行为一个正整数n (1<=n<=10)。
    接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。

    Output

    输出n行,每行为输入对应的八进制正整数。

    Sample Input

    2
    39
    123ABC

    Sample Output

    71
    4435274

    Hint

    提示
    先将十六进制数转换成某进制数,再由某进制数转换成八进制。

    Source

    蓝桥杯
     
    先将十六进制转化成二进制,再将二进制转化成八进制,开始在转化成八进制的时候没有判断最后只剩两位或者一位的情况导致WA几次。。。。
     
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<queue>
    #include<cmath>
    #include<algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    int s2[10000000],tmp[10000000];
    int main() {
        int T;
        cin >> T;
        while( T -- ) {
            int n = 0, j = 0;
            string s1;
            cin >> s1;
            memset( s2, 0, sizeof(s2) );
            memset( tmp, 0, sizeof(tmp) );
            for( int i = s1.length()-1; i >= 0 ; i -- ) {
                int t, k = s1.length() - i - 1;
                if( s1[i] >= 'A' && s1[i] <= 'F' ) {
                    t = s1[i] - 'A' + 10;
                } else {
                    t = s1[i] - '0';
                }
                int cnt = 0;
                //debug(t);
                while( t ) {
                    if( t % 2 == 1 ) {
                        if( j < 4*k+cnt ) {
                            j = 4*k+cnt;
                        }
                    }
                    s2[4*k+cnt] = t % 2;
                    t /= 2;
                    cnt ++;
                }
            }
            int k = 0;
            for( int i = 0; i <= j; i ++ ) {
                if( ( i + 1) % 3 == 0 ) {
                    tmp[k++] = s2[i]*4 + s2[i-1]*2 + s2[i-2];
                } else if( i == j ) {
                    if( ( i + 1) % 3 == 2 ) {
                        tmp[k++] = s2[i]*2 + s2[i-1];
                    } else if( ( i + 1 ) % 3 == 1 ) {
                        tmp[k++] = s2[i];
                    }
                }
            }
            for( int i = k-1; i >= 0; i -- ) {
                cout << tmp[i];
            }
            cout << endl;
        }
        return 0 ;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    toj 2975 Encription
    poj 1797 Heavy Transportation
    toj 2971 Rotating Numbers
    zoj 2281 Way to Freedom
    toj 2483 Nasty Hacks
    toj 2972 MOVING DHAKA
    toj 2696 Collecting Beepers
    toj 2970 Hackle Number
    toj 2485 Card Tric
    js页面定位,相关几个属性
  • 原文地址:https://www.cnblogs.com/l609929321/p/8530889.html
Copyright © 2020-2023  润新知