HDOJ 4054 Hexadecimal View

~~~题目链接~~~


思路:模拟


#include <stdio.h>
#include <string.h>
int len = 0;
char str[5002];
void print(int cur)
{
    int i = 0, j = 0;
    for(i = cur; i<cur+16; i++)
    {
        if(j == 2) printf(" "), j = 0;
        if(i<len)
            printf("%.2x", str[i]);
        else
            printf("  ");
        j++;
    }
    printf(" ");
    for(i = cur; i<cur+16; i++)
    {
        if(i>=len) break;
        if(str[i]>='a' && str[i]<='z') printf("%c", str[i]+'A'-'a');
        else if(str[i]>='A' && str[i]<='Z') printf("%c", str[i]+'a'-'A');
        else printf("%c", str[i]);
    }
    printf("\n");
}

int main()
{
    int i = 0, j = 0, c = 0;
    while(gets(str))
    {
        len = strlen(str);
        c = len/16;
        for(i = 0, j = 0; i<=c; i++)
        {
            if(j == len) break;
            printf("%.4x: ", j);
            print(j);
            j += 16;
        }
    }
    return 0;
}



你可能感兴趣的:(HDOJ 4054 Hexadecimal View)