HDU-4054.Hexadecimal View(模拟,十六进制转换)

26 篇文章 0 订阅

M - M

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 4054

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

 

 

稍微注意下输出的格式就好啦~~ 这是我比赛后补的题目,我队友比赛的时候开的还是很快的,感谢队友哈哈哈

【题意】

中间四位四位输出的是两个字符为一组的ascii码转换成16进制数后的数,然后后面每一行输出16个字符(大小写互换一下)

【通过代码】

#include<cstdio>
#include<iostream>
#include<cstring>
const int maxn = 1e5 + 5;
using namespace std;
char str[5000];
int main()
{
    int t = 0,len;
    while(cin.getline(str,maxn))
    {
        t = 0;
        len = strlen(str);
        for(t = 0;t < len;t += 16)
        {
            printf("%04x: ",t);
            for(int i = t;i< t + 16;i += 2)
            {
                if(i != t )
                    printf(" ");//除了第一个,后面每两个输出前都有个空格
                if(i < len) printf("%02x",str[i]);
                else    printf("  ");//如果后面没有字符了,就用2个空格代替占位
                if(i + 1 < len) printf("%02x",str[i+1]);
                else    printf("  ");
            }
            cout<<" ";
            for(int i=t;i<t+16&&i<len;i++)
            {
                if(str[i]>='a'&&str[i]<='z') printf("%c",toupper(str[i]));
                else if(str[i]>='A'&&str[i]<='Z')  printf("%c",tolower(str[i]));
                else printf("%c",str[i]);
            }
            cout<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值