hdu 4054 Hexadecimal View(字符串)

题意 :

输出分三列。
第一列 : 0000: 表示当前语句的第1行 0010:表示当前语句的第二行。 以为规定一行只能输出16个字节, 所以代码中的一行可能要多行输出。 比如 该行代码一行有110字节 110 = 6 * 16 + 14就要输出 06e0:
第二列: 一个字节8位, 一个16进制的数 4位, 所以每两个 16进制数就代表一个字符。
不满16个字节,要输出空格。
第三列: 把代码的的大小写互换。 每16个就要换行, 输出完一行代码也要换行(后面无多余的空格)。
列与列之间有一个空格。

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 5000;
char buf[N];
int main() {
    memset(buf, 0, sizeof(buf));
    while(gets(buf)) {
        int len = strlen(buf);
        for(int i = 0; i < len; i += 16) {
            printf("%04x: ", (int)i);
            for(int j = i; j < i+16; j += 2) {
                if(buf[j] == '\0') {
                    printf("  ");
                }else {
                    printf("%x", (int)buf[j]);
                }
                if(buf[j+1] == '\0') {
                    printf("   ");
                }else {
                    printf("%x ",(int)buf[j+1]);
                }
            }
            for(int j = i; j < i+16 && j < len; j++) {
                if(buf[j] >= 'a' && buf[j] <= 'z') {
                    putchar(toupper(buf[j]));
                }else {
                    putchar(tolower(buf[j]));
                }
            }
            puts("");
        }
        memset(buf, 0, sizeof(buf));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值