OpenJudge百炼-2798-2进制化为16进制-C语言-数制转换

来源:互联网 发布:唐朝在日本 知乎 编辑:程序博客网 时间:2024/06/05 19:10
描述:
输入一个2进制的数,要求输出该2进制数的16进制表示。
在16进制的表示中,A-F表示10-15
输入:
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个以0和1组成的字符串,字符串长度至少是1,至多是10000
输出:
n行,每行输出对应一个输入。
样例输入:
2
100000
111
样例输出:
20

7

/*****************************************************文件名:百炼-2798**Copyright (c) 2015-2025 OrdinaryCrazy**创建人:OrdinaryCrazy**日期:20170916**描述:百炼2798参考答案**版本:1.0****************************************************/#include <stdio.h>#include <string.h>int main(){    int n,len,head,temp;    char num[10005];    scanf("%d",&n);    while(n--)    {        num[0] = num[1] = num[2] = '0';        scanf("%s",&num[3]);        len = strlen(&num[3]);        temp = len % 4;        len += (4 - temp)%4;        head = 3 - (4 - temp)%4;        while(head < len)        {            temp = (num[head] - '0')*8 + (num[head + 1] - '0')*4 + (num[head + 2] - '0')*2 + (num[head + 3] - '0');            temp > 9 ? printf("%c",'A' + temp - 10) : printf("%d",temp);            head += 4;        }        printf("\n");    }    return 0;}


阅读全文
0 0
原创粉丝点击