Hexadecimal View


Time Limit: 2 Seconds       Memory Limit: 65536 KB

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

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

Sample Output

  1. 0000: 4865 7820 4475 6d70 hEX dUMP
  2. 0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE <CSTDIO
  3. 0010: 3e >
  4. 0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w
  5. 0010: 6f72 6c64 215c 6e22 293b ORLD!\N");
  6. 0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN
  7. 0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU
  8. 0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W
  9. 0030: 6f72 6473 ORDS
           
题目大意:
这个题目在看了A题觉得太复杂了,就开始看这个了。感觉应该是个比较简单的模拟,但是题目挂在HDU上面有问题。
  1. 0000: 4865 7820 4475 6d70 hEX dUMP
  2. 0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE &lt;CSTDIO
  3. 0010: 3e >
  4. 0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w
  5. 0010: 6f72 6c64 215c 6e22 293b ORLD!\N");
  6. 0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN
  7. 0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU
  8. 0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W
  9. 0030: 6f72 6473 ORDS

这是比赛时候的输出,当时看左简括号表示有点不解,其他的都想地差不多了。

      解题思路:给你一串字母,然后每次对16个字母转换成int再转化成16进制的,弄一行。每次输出地址0010,0020,最后一位是无效位。前三位表示地址。中间是dump,输出的是每个字母转换成16进制两位输出,每两个字母后跟一个空格,最后面跟的就是大小写转换即可。

     总结:开始WA了以后,我一直以为是地址的问题,长度4096会输出0000.4096 printable characters,这个还没有除以16,那才是地址。肯定这地方没错。每次交之前应该考虑特殊数据,比如长度为16的。。。

     题目地址:Hexadecimal View

AC代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #define INF 0x3f3f3f3f
  13. #define maxn 105
  14. using namespace std;
  15. char a[5000]; //原串
  16. char b[5]; //最前面的地址
  17. int len,t1;
  18. char tex[20]; //最后面的大小写转换
  19. char dum[3]; //中间的字母转换成16进制的
  20.  
  21. void addr(int x) //最前面的地址
  22. {
  23. int i,p;
  24. p=0;
  25. int t[5];
  26. memset(t,0,sizeof(t));
  27. while(x)
  28. {
  29. t[p++]=x%16;
  30. x/=16;
  31. }
  32. for(i=0;i<=2;i++)
  33. {
  34. if(t[2-i]<10)
  35. b[i]=t[2-i]+'0';
  36. else
  37. b[i]=t[2-i]-10+'a';
  38. }
  39. /*for(i=2;i>=0;i--)
  40. cout<<t[i]<<" ";
  41. cout<<endl;*/
  42. }
  43.  
  44. void dump(char x) //中间的字母转换成16进制的
  45. {
  46. int i,p;
  47. p=0;
  48. int t[5];
  49. memset(t,0,sizeof(t));
  50. int l=int(x);
  51. while(l)
  52. {
  53. t[p++]=l%16;
  54. l/=16;
  55. }
  56. for(i=0;i<=1;i++)
  57. {
  58. if(t[1-i]<10)
  59. dum[i]=t[1-i]+'0';
  60. else
  61. dum[i]=t[1-i]-10+'a';
  62. }
  63. }
  64.  
  65. void text(char x) //最后面的大小写转换
  66. {
  67. /*if(x=='<') //坑
  68. {
  69. tex[t1++]='&';
  70. tex[t1++]='l';
  71. tex[t1++]='t';
  72. tex[t1++]=';';
  73. }
  74. else*/
  75. if(x>='a'&&x<='z')
  76. {
  77. tex[t1++]=x-32;
  78. }
  79. else if(x>='A'&&x<='Z')
  80. tex[t1++]=x+32;
  81. else
  82. tex[t1++]=x;
  83. }
  84.  
  85. int main()
  86. {
  87. int i,pp,j;
  88. while(gets(a))
  89. {
  90. len=strlen(a);
  91. pp=len/16;
  92. b[3]='0',b[4]='\0'; //地址最后一位无效直接为0
  93. for(i=0;i<pp;i++)
  94. {
  95. addr(i);
  96. cout<<b<<": "; //最前面的地址
  97. t1=0;
  98. for(j=i*16;j<i*16+16;j++)
  99. {
  100. dump(a[j]);
  101. text(a[j]);
  102. cout<<dum[0]<<dum[1];
  103. if(j&1) cout<<" "; //每两位一个空格
  104. }
  105. tex[t1]='\0';
  106. cout<<tex<<endl; //大小写转换
  107. }
  108.  
  109. t1=0;
  110. if(len>pp*16) //开始这个地方写的>=,感谢吉吉debug
  111. {
  112. addr(i);
  113. cout<<b<<": ";
  114. for(j=i*16;j<len;j++)
  115. {
  116. dump(a[j]);
  117. text(a[j]);
  118. cout<<dum[0]<<dum[1];
  119. if(j&1) cout<<" ";
  120. }
  121. tex[t1]='\0';
  122. for(j=len;j<i*16+16;j++)
  123. {
  124. cout<<" "; //补空格
  125. if(j&1) cout<<" ";
  126. }
  127. cout<<tex<<endl;
  128. }
  129. }
  130.  
  131. /*int len=4096;
  132. addr(len);
  133. cout<<b<<endl;*/
  134. return 0;
  135. }
  136.  
  137. //30ms 192KB

ZOJ 3542 2011大连现场赛D题(简单模拟)的更多相关文章

  1. HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5112 解题报告:扫一遍 #include<cstdio> #include<cstr ...

  2. zoj 3820(2014牡丹江现场赛B题)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374 思路:题目的意思是求树上的两点,使得树上其余的点到其中一个点的 ...

  3. zoj 3827(2014牡丹江现场赛 I题 )

    套公式 Sample Input 33 bit25 25 50 //百分数7 nat1 2 4 8 16 32 3710 dit10 10 10 10 10 10 10 10 10 10Sample ...

  4. zoj 3819(2014牡丹江现场赛 A题 )

    题意:给出A班和B班的学生成绩,如果bob(A班的)在B班的话,两个班级的平均分都会涨.求bob成绩可能的最大,最小值. A班成绩平均值(不含BOB)>A班成绩平均值(含BOB) &&a ...

  5. HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)

    Isabella's Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. 2013杭州现场赛B题-Rabbit Kingdom

    杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...

  7. Gym101981D - 2018ACM-ICPC南京现场赛D题 Country Meow

    2018ACM-ICPC南京现场赛D题-Country Meow Problem D. Country Meow Input file: standard input Output file: sta ...

  8. HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题

    第一年参加现场赛,比赛的时候就A了这一道,基本全场都A的签到题竟然A不出来,结果题目重现的时候1A,好受打击 ORZ..... 题目链接:http://acm.hdu.edu.cn/showprobl ...

  9. HDU 4816 Bathysphere (2013长春现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 2013长春区域赛的D题. 很简单的几何题,就是给了一条折线. 然后一个矩形窗去截取一部分,求最 ...

随机推荐

  1. c++, class的大小

    不为类.对象的函数分配空间: 在类中如果有virtual声明的虚函数,则会隐藏一个指针,该指针指向虚函数表,这对于纯虚函数也是一样: 对于虚继承,还有一个指向父类的指针,该指针为指向虚基类的指针(Po ...

  2. WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下?

    原文 WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下? 一.问题的提出 偶然发现,Winform里的WebBrowser和IE实际安装的版本似乎并不同步,很有趣! 下 ...

  3. 【图像处理】Gabor过滤器

    Gabor内核参考wiki 使用实数Real的公式计算核函数代码: Mat getGaborFilter(float lambda, float theta, float sigma2,float g ...

  4. EasyUI - DataGrid 组建 - [ 删除,修改 ]

    效果: html代码: <div style="padding-top: 50px; width: 800px; margin: 0 auto;"> <!--使用 ...

  5. TODO管理工具TaskWarrior (跨平台C++代码)

    Taskwarrior 是一个基于命令行的 TODO 列表管理工具.主要功能包括:标签.彩色表格输出.报表和图形.大量的命令.底层API.多用户文件锁等功能. http://www.oschina.n ...

  6. C++学习之路—多态性与虚函数(一)利用虚函数实现动态多态性

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多态性是面向对象程序设计的一个重要特征.顾名思义 ...

  7. asp.net 检查文件夹和文件是否存在

    原文  asp.net 检查文件夹和文件是否存在 允许 path 参数指定相对或绝对路径信息. 相对路径信息被解释为相对于当前工作目录. 检查该目录是否存在之前,从 path 参数的末尾移除尾随空格. ...

  8. javascript addEventListener方法

    addEventListener是一个侦听事件并处理相应的函数. DOM方法 addEventListener() 和 removeEventListener()是用来分配和删除事件的函数. 这两个方 ...

  9. hdu 4706

    注意一点 空的地方打空格而不是空字符,我因为这wa了一次... #include<cstdio> #include<cstring> #include<cstdlib&g ...

  10. Opencv实现图像的灰度处理,二值化,阀值选择

    前几天接触了图像的处理,发现用OPencv处理确实比較方便.毕竟是非常多东西都封装好的.可是要研究里面的东西,还是比較麻烦的,首先,你得知道图片处理的一些知识,比方腐蚀,膨胀,仿射,透射等,还有非常多 ...