首页 > ACM题库 > HDU-杭电 > hdu 4054-hexadecimal view-字符串-[解题报告]hoj
2015
04-16

hdu 4054-hexadecimal view-字符串-[解题报告]hoj

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
模拟,需注意最后一列不用补齐,还有注意当字符个数为16的整数倍时的行数。
language:c++
code:
#include<iostream>
#include<cctype>
#include<cstring>
#include<cstdio>
using namespace std;

int main()
{
    char s[4100];
    while(gets(s))
    {
        int len=strlen(s);
        int line;
        if(len%16==0)line=len/16;
        else line=len/16+1;
        for(int i=0;i<line;i++)
        {
            int count=0;
            printf("%.4x: ",i<<4);
            for(int j=i*16;s[j]&&j<i*16+16;j++)
            {
                printf("%.2x",s[j]);
                count+=2;
                if(j&1){putchar(' ');count++;}
            }
            while(count++<40)putchar(' ');
            for(int j=i*16;s[j]&&j<i*16+16;j++)
            {
                if(islower(s[j]))
                putchar(toupper(s[j]));
                else
                putchar(tolower(s[j]));
            }
            printf("\n");
        }
    }
    return 0;
}
 

版权声明:本文为博主原创文章,未经博主允许不得转载。


  1. 因为是要把从字符串s的start位到当前位在hash中重置,修改提交后能accept,但是不修改居然也能accept

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  3. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.