1. 程式人生 > >hdu 4054 Hexadecimal View(字串)

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; }