hdu 4054 字符串处理

现场赛中最水的题,发现这题时比赛已经进行了半个小时,可是我打了半个小时还没有搞定,看着旁边的队伍一个个地升起气球,更加紧张,后面还有一点小问题干脆让海峰写了。今天发现杭电加了现场赛的题目,20分钟就把这题给过了。看来赛场上的心理素质还得训练,不然正式比赛的时候难以发挥出最好的水平~

/*
* hdu4054/win.cpp
* Created on: 2011-10-6
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX_LEN = 4200;
char ori[MAX_LEN], newstr[MAX_LEN];

void work() {
strcpy(newstr, ori);
int len = strlen(ori);
for (int i = 0; i < len; i++) {
if (newstr[i] >= 'A' && newstr[i] <= 'Z') {
newstr[i] += 32;
} else if (newstr[i] >= 'a' && newstr[i] <= 'z') {
newstr[i] -= 32;
}
}
int I = 0;
while (I * 16 + 16 <= len) {
printf("%04x: ", I * 16);
for (int j = 0; j < 16; j += 2) {
printf("%x%x ", ori[I * 16 + j], ori[I * 16 + j + 1]);
}
for (int j = 0; j < 16; j++) {
putchar(newstr[I * 16 + j]);
}
putchar('\n');
I++;
}
if (I * 16 < len) {
int J = len - I * 16;
printf("%04x: ", I * 16);
for (int j = 0; j < J - 1; j += 2) {
printf("%x%x ", ori[I * 16 + j], ori[I * 16 + j + 1]);
}
if (J % 2 == 1) {
printf("%x ", ori[I * 16 + J - 1]);
}
int K = 16 - J;
for (int j = 0; j < K - 1; j += 2) {
printf(" ");
}
for (int j = 0; j < J; j++) {
putchar(newstr[I * 16 + j]);
}
putchar('\n');
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
while (gets(ori)) {
work();
}
return 0;
}



posted @ 2011-10-06 15:41  moonbay  阅读(231)  评论(0编辑  收藏  举报