博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4054 Hexadecimal View【模拟】【字符串处理】
阅读量:7020 次
发布时间:2019-06-28

本文共 3438 字,大约阅读时间需要 11 分钟。

Hexadecimal View

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


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

最后一个字符的转换完成后不需要补全,直接换行,注意边界条件的判断

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define ms(a,b) memset(a,b,sizeof(a))using namespace std;const int maxn = 200010;const int mod = 1e9 + 7;const double pi = acos(-1.0);typedef pair
P;typedef long long ll;typedef unsigned long long ull;string tohex(char c){ string ans = "00"; int p = c, cnt = 1; while (p) { if (p % 16 < 10) { ans[cnt] = p % 16 + '0'; } else { ans[cnt] = p % 16 - 10 + 'a'; } cnt--; p /= 16; } return ans;}struct Line { string a; void operator++() { bool p = 1; int cur = 2; while (p) { p = 0; if (a[cur] != '9'&&a[cur] != 'f') a[cur]++; else { if (a[cur] == '9') a[cur] = 'a'; else { a[cur] = '0'; p = 1; } } cur--; } }};int main(){ string a; while (getline(cin, a)) { Line line; line.a = "0000"; int i = 0; for (i = 0; i < a.length(); i++) { if (i % 16 == 0)//判断是否换行 { if (i != 0) { cout << endl; } cout << line.a << ": "; ++line; } cout << tohex(a[i]);//前面输出 if (i % 2 != 0)//判断前面的空格 { cout << " "; } if (i&&(i+1) % 16 == 0 || i == a.length() - 1) { if ((i + 1) % 16 != 0) //补前面空格 { for (int j = i % 16 + 1; j <= 15; j++) { cout << " "; if (j % 2 != 0) cout << " "; } } for (int j = i - i % 16; j <= i; j++)//后面转换 { if (a[j] >= 'A'&&a[j] <= 'Z') { cout << (char)(a[j] - 'A' + 'a'); } else if (a[j] >= 'a'&&a[j] <= 'z') { cout << (char)(a[j] - 'a' + 'A'); } else cout << a[j]; } } } cout << endl; }}

代码写的好丑,可以参照写的比较精简。

转载于:https://www.cnblogs.com/Archger/p/8451631.html

你可能感兴趣的文章
Oracle EBS-SQL (BOM-9):检查系统BOM总数.sql
查看>>
PhpStorm 常用快捷键
查看>>
RDP 协议组件 X.224 在协议流中发现一个错误并且中断了客户端连接
查看>>
CentOS yum 源的配置与使用
查看>>
页面内部实现跳转
查看>>
[转] 看懂UML类图和时序图
查看>>
[转] Initial Impressions on GraphQL & Relay
查看>>
su认证失败
查看>>
Java-javaFx库运用-时钟显示
查看>>
【Dart学习】-- Dart之extends && implements && with的用法与区别
查看>>
常用十六进制颜色对照表代码查询
查看>>
扩展性很好的一个分页存储过程
查看>>
如何提升SharePoint 2010的性能
查看>>
asp.net 导出excel 中文乱码解决方法 (转)
查看>>
C#泛型学习笔记
查看>>
char-2
查看>>
软件工程(2018)第二次作业
查看>>
block
查看>>
react-native Image resizeMode
查看>>
温湿度系统(花葵、库房检测)
查看>>