HDU 4054

http://acm.hdu.edu.cn/showproblem.php?pid=4054

模拟题,对一个字符串的每个字符输出16进制表示的数字,每行处理16个字符,后面再把这16个字符输出,大小写互换

利用%x可以直接输出16进制,轻松秒掉

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <set>

using namespace std;

char str[6000];

int main(){
    while(gets(str)){
        int len=strlen(str);
        for(int i=0;i<len;i+=16){
            printf("%04x:",i);
            int odd=1;
            int cnt=0;
            for(int j=i;j<len && j<i+16;j++){
                if(odd&1){
                    odd=0;
                    printf(" %x",str[j]);
                    cnt+=3;
                }
                else{
                    odd=1;
                    printf("%x",str[j]);
                    cnt+=2;
                }
            }
            for(int j=0;j<41-cnt;j++)
                putchar(' ');
            for(int j=i;j<len && j<i+16;j++){
                if(str[j]>='a' && str[j]<='z'){
                    printf("%c",str[j]-'a'+'A');
                }
                else if(str[j]>='A' && str[j]<='Z'){
                    printf("%c",str[j]-'A'+'a');
                }
                else{
                    printf("%c",str[j]);
                }
            }
            putchar('
');
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xiaohongmao/p/4133218.html