Hexadecimal View HDU 4054 简短代码 详解



Hexadecimal View

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1837    Accepted Submission(s): 753


Problem Description
Hexadecimal is very important and useful for computer programmers. You are requested to provide a hexadecimal view for given data. The hexadecimal view is made up of one or more rows. Every row except the last one represents 16 characters. Each row consists of three columns separated by a space:

* addr: the 4-digit hexadecimal beginning address of this row.
* dump: the hexadecimal representation of this row, separating every two characters by a whitespace. If there are less than 16 characters in the last row, pad it with spaces.
* text: the ASCII translation of this row, with uppercase characters converted to lowercase and lowercase characters converted to uppercase.
Use lowercase for the letter digits. See sample for more details.
 

Input
There are multiple test cases. Each line is a test case. The line is made up of no less than 1 and no more than 4096 printable characters including spaces.
 

Output
For each test case, output its hexadecimal view. Do not output any extra spaces after the last character of text.
 

Sample Input
   
   
   
   
Hex Dump #include <cstdio> printf("Hello, World!\n"); main = do getLine >>= print . sum . map read . words
 

Sample Output
   
   
   
   
0000: 4865 7820 4475 6d70 hEX dUMP 0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE <CSTDIO 0010: 3e > 0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w 0010: 6f72 6c64 215c 6e22 293b ORLD!\N"); 0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN 0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU 0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W 0030: 6f72 6473 ORDS
 

Author
WU, Zejun
 

Source
2011 Asia Dalian Regional Contest
 

Recommend



lcy   |   We have carefully selected several similar problems for you:   4055  4057  4056  4059  4053 


注意:行号最后一位是恒等于0,且为16进制,如     02a0。。。。

这个题纠结了许久,一开始WA了n次,而且调了很久都没弄出错误在哪,后来干脆换了个简单的思路:Ac代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

char input[4100];

int main()
{
    int i,j,len,k;

    while(gets(input) != NULL)
    {
        len = strlen(input);
        for(i = 0;i < len;i+=16)//最外层循环,每次使标记前进16个字符----因为每输出16个字符为一个循环
        {
            printf("%04x:",i);//i永远是16的整数倍,可以确保以十六进制输出时最后一位为0————举个例子就想清了——%04X加0是要0补齐前面的位置
            for(j = i;j < i + 16;j++)//输出字符
            {
                if(j % 2 == 0) printf(" ");//数字之间的空格
                if(j < len)printf("%02x",input[j]);
                else printf("  ");     //j < len,说明输出还未满16个字符就没有字符了,那就用空格代替
            }
            printf(" ");//最后追加一个空格
            
            for(k = i;k < i + 16 && k < len;k++)//对字符的大小写处理
            {
                if(isalpha(input[k]) == 2) printf("%c",input[k] - 'a' + 'A');
                else if(isalpha(input[k]) == 1) printf("%c",input[k] - 'A' + 'a');
                else printf("%c",input[k]);
            }
            printf("\n");
        }
    }
    return 0;
}
/////
/////

WA代码如下(很复杂,注释就不写了,抽空再反过来看一下)


#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char input[4100],temp[4100];
int line;


int main()
{
    int i,j,len,k,m;

    while(gets(input) != NULL)
    {
        len = strlen(input);

        for(i = 0;i < len;i++)
        {
            if(isalpha(input[i]) == 1) temp[i] = input[i] + 32;
            else if(isalpha(input[i]) == 2) temp[i] = input[i] - 32;
            else temp[i] = input[i];
        }

        printf("0000:");
        line = 0;
        k = 5;

        for(i = 0;i < len;i++)
        {
            if(i % 16 == 0 && i)
            {
                printf(" ");
                for(j = i - 16; j < i;j++)
                    printf("%c",temp[j]);
                line++;
                printf("\n%03x0:",line);
                k = 5;
            }

            if(i % 2 == 0) {printf(" ");k++;}
            printf("%02x",input[i]);k+=2;
        }

        if(len % 16 != 0)
        {
            printf(" ");k++;
            for(m = 0;m < 45 - k + 1;m++)
                printf(" ");
            for(j = len - len % 16;j < len;j++)
                printf("%c",temp[j]);
        }
        printf("\n");
        memset(input,'\0',sizeof(input));
        memset(temp,'\0',sizeof(temp));
    }
    return 0 ;
}


你可能感兴趣的:(ACM,HDU,杭电)