您的位置:首页 > 其它

大连赛区现场赛D题 ZOJ 3542 Hexadecimal View

2011-10-02 20:53 369 查看
大连赛区现场赛最水的一题吧,没什么好说的,简单的模拟题,对于每一行内容,每次取16个字符出来进行处理,输出分成三部分处理会清晰一点。稍微注意下细节就是了

代码写的比较丑,大牛们就飞过吧~

#include <string>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cctype>
#include <cstring>
#include <algorithm>

using namespace std;

string s;
int tot;

void print_x(string s1)
{
int cnt = 0;
for (int i=0; i<s1.length(); i++)
{
printf("%02x", int(s1[i]));
cnt++;
if (!(cnt&1)) printf(" ");
}
while (cnt < 16)
{
printf("  ");
cnt++;
if (!(cnt&1)) printf(" ");
}
}
void print_s(string s1)
{
for (int i=0; i<s1.length(); i++)
if (isalpha(s1[i]))
{
if (isupper(s1[i])) printf("%c", tolower(s1[i]));
else printf("%c", toupper(s1[i]));
}
else printf("%c", s1[i]);
printf("\n");

}
int main()
{
string st;
while (getline(cin, s))
{
tot = 0;
while (s != "")
{
printf("%04x: ", tot);
if (s.length() >= 16)
{
st = s.substr(0, 16);
s.erase(0, 16);
}
else
{
st = s;
s.clear();
}
print_x(st);
print_s(st);
tot+=16;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: