原题 http://acm.hdu.edu.cn/showproblem.php?pid=4054
题目:
Hexadecimal View
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2370 Accepted Submission(s): 957
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
: d7 hEX dUMP
: e63 c75 c f #INCLUDE <CSTDIO
: e >
: e c6c f2c PRINTF("hELLO, w
0010: 6f72 6c64 215c 6e22 293b ORLD!\N");
: d61 e d f2 c e MAIN = DO GETlIN
: e3e d2 e e2 E >>= PRINT . SU
: d2 e2 d61 e M . MAP READ . W
: f72 ORDS
思路:
输出样例在内存中存储的十六进制数和大小写转换过的内容。
注意:前面的序号也是十六进制数。
代码:
#include <iostream>
#include"string.h"
#include"cstdio"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#include"queue"
#include"stack"
using namespace std;
typedef long long int lint;
int main()
{
char s[];
while(gets(s))
{
int len=strlen(s);
int r;
if(len%16==) r=len/;
else r=len/+;
int j=;
int k=;
for(int i=; i<r; i++)
{
printf("%03x0: ",i);
for(; j<r*16; j++)
{
if(j<len) printf("%x",s[j]);
else printf(" ");
if(j%2==) printf(" ");
if((j+)%16==)
{
j++;
break;
}
}
for(; k<len; k++)
{
if(s[k]>='A'&&s[k]<='Z')
printf("%c",s[k]+);
else if (s[k]>='a'&&s[k]<='z')
printf("%c",s[k]-);
else
printf("%c",s[k]);
if(s[k]=='\0') break;
if((k+)%16==)
{
k++;
break;
}
}
printf("\n");
}
}
return ;
}