hdu 4054 Hexadecimal View

来源:互联网 发布:js dom 添加属性 编辑:程序博客网 时间:2024/04/20 12:57
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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 dUMP0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE &lt;CSTDIO0010: 3e                                      >0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w0010: 6f72 6c64 215c 6e22 293b                ORLD!\N");0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W0030: 6f72 6473                               ORDS


题意 : 输出分三列。

第一列 : 0000: 表示当前语句的第1行  0010:表示当前语句的第二行。  以为规定一行只能输出16个字节, 所以代码中的一行可能要多行输出。 比如 该行代码一行有110字节  110 = 6 * 16 + 14就要输出 06e0:

第二列:  一个字节8位, 一个16进制的数 4位, 所以每两个 16进制数就代表一个字符。

不满16个字节,要输出空格。

第三列: 把代码的的大小写互换。 每16个就要换行, 输出完一行代码也要换行(后面无多余的空格)。

列与列之间有一个空格。


思路 :读入一行,输出一行。 暴力,耐心对照。


#include <stdio.h>#include <string.h>#include <ctype.h>//转化余数为对应的的字母char f(int num) {if(num >= 0 && num < 10)return num + '0';elsereturn (num - 10) + 'a';}//转化代码为两个16进制的数void char_to_number(char a) {char c = 0, ch[2];int num = a - c;ch[0] = f(num / 16);ch[1] = f(num % 16);printf("%c%c", ch[0], ch[1]);}//大小写转化char g(char ch) {if(isupper(ch))ch += 32;else if(islower(ch))ch -= 32;return ch;}//根据代码行数输出第一列void h(int num) {int temp = 256, i;for(i = 0; i < 3; ++i) {printf("%c", f(num / temp));num -= (num / temp) * temp;temp /= 16;}printf("0: ");}int main() {char a = 0, ch[4096], *p = ch;int i, count, j, len, k, sub_len, finish;while(gets(p)) {finish = 0;sub_len = strlen(p);len = (sub_len - 1) / 16; //确定行数//双重循环依次降刚读入的代码分割输出for(k = 0; k <= len; ++k) {h(k);//输出第一部分for(i = 1; i <= 16; ++i) {if(i > sub_len) {//补空格if(i % 2 == 0)printf("   ");elseprintf("  ");if(i == 16) {for(j = finish; ch[j] != '\0'; ++j)printf("%c", g(ch[j]));break;}continue;}count = 0;char_to_number(ch[finish+i-1]);if(i % 2 == 0)printf(" ");if(i == 16) {//输出第三部分for(j = finish; j < finish + 16; ++j)printf("%c", g(ch[j]));finish += 16;}}printf("\n");sub_len -= 16;}}}


原创粉丝点击