蓝桥杯练习题—— 十六进制转八进制

/*
*************************************************************
    Title: 蓝桥杯练习题—— 十六进制转八进制
*************************************************************
    Date:2014/03/13
*************************************************************
    author:刘旭
*************************************************************
*/
#include
#include
int x2i(char c)
{
    if (c >= 'A')
        return c - 55;
    else
        return c - 48;
}

int main()
{
    int i, j, n;
    scanf("%d", &n);
    while(n--)
    {
        char a[100001];
        scanf("%s", a);
        char* p = a;
        int len = strlen(p);
        if (len % 3 == 1)
        {
            printf("%o", x2i(p[0]));
            j = 1;
        }
        else if (len % 3 == 2)
        {
            printf("%o", x2i(p[0])*16+x2i(p[1]));
            j = 2;
        }
        else
        {
            printf("%o", x2i(p[0])*256+x2i(p[1])*16+x2i(p[2]));
            j = 3;
        }
        for (; j < len; j += 3)
            printf("%04o", x2i(p[j])*256+x2i(p[j+1])*16+x2i(p[j+2]));
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(蓝桥杯练习)