题解 | #字符串合并处理#

字符串合并处理

http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void sort(char *ptr)
{
    int len = strlen(ptr);
    for(int i = 0; i < len; i += 2)
    {
        for(int j = i+2; j < len; j += 2)
        {
            if(ptr[i] > ptr[j])
            {
                char ch = ptr[i];
                ptr[i] = ptr[j];
                ptr[j] = ch;
            }
        }
    }
}

unsigned char str2hex(char ch)
{
    if(isdigit(ch))
        return ch - '0';
    else
        return tolower(ch) - 'a' + 10;
}

char hex2str(unsigned char hex)
{
    if(hex < 10)
        return '0' + hex;
    else
        return 'A' + hex - 10;
}

unsigned char convertch(unsigned char ch)
{
    unsigned char ret = 0x00;
    for(int i = 0; i < 4; i++)
    {
        if(ch & 0x1)
            ret |= 0x1;
        ch >>= 1;
        ret <<= 1;
    }
    ret >>= 1;
    return ret;
}

void convert(char *ptr)
{
    int len = strlen(ptr);
    for(int i = 0; i < len; i++)
    {
        if(isxdigit(ptr[i]))
            ptr[i] = hex2str(convertch(str2hex(ptr[i])));
    }
}

int main(void)
{
    int i = 0;
    char str[512], output[1024];
    while(scanf("%s",str) != EOF)
    {
        if(i == 0)
        {
            memset(output, '\0', sizeof(output));
            memcpy(output, str, strlen(str));
            i++;
            continue;
        }
        else
        {
            i = 0;
            strcat(output, str);
            sort(output);
            sort(output+1);
//             printf("%s\n", output);
            convert(output);
            printf("%s\n", output);
        }
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:25
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务