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

字符串合并处理

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

#include <stdio.h>
#include <string.h>
int chartype(char c) {
    if (c >= '0' && c <= '9')
        return 1;
    else if (c >= 'a' && c <= 'f')
        return 2;
    else if (c >= 'A' && c <= 'F')
        return 3;
    else
        return 0;
}

char transferchar(char c) {
    int chrtyer = chartype(c);
    char temp = c;
    char c1, c2, c3, c4;
    if (chrtyer == 1)
        c = c - '0';
    else if (chrtyer == 2)
        c = c - 'a' + 10;
    else if (chrtyer == 3)
        c = c - 'A' + 10;
    if (chrtyer) {
        c4 = c / 8;
        c = c % 8;
        c3 = c / 4;
        c = c % 4;
        c2 = c / 2;
        c = c % 2;
        c1 = c;
        temp = c1 * 8 + c2 * 4 + c3 * 2 + c4;
        if (temp < 10)
            temp = '0' + temp;
        else
            temp = temp + 'A' - 10;
    }
    return temp;
}
void strsort(char s[]) {
    int slen = strlen(s);
    int slen1, slen2 = slen / 2;
    char temp;
    if (slen % 2)
        slen1 = slen / 2 + 1;
    else
        slen1 = slen / 2;
    for (int i = 0; i < slen1 * 2 - 2; i += 2) {
        for (int j = 0; j < slen1 * 2 - 2 - i; j += 2) {
            if (s[j] > s[j + 2]) {
                temp = s[j];
                s[j] = s[j + 2];
                s[j + 2] = temp;
            }
        }
    }
    for (int i = 1; i < slen2 * 2 - 1; i += 2) {
        for (int j = 1; j < slen2 * 2 - 1 - i; j += 2) {
            if (s[j] > s[j + 2]) {
                temp = s[j];
                s[j] = s[j + 2];
                s[j + 2] = temp;
            }
        }
    }
}

int main() {
    char str1[101], str2[101];
    scanf("%s %s", str1, str2);
    int len = 300;//strlen(str1) + strlen(str2) + 1;
    char str[len];
    strcat(str, str1);
    strcat(str, str2);
    strsort(str);
    int slen = strlen(str);
    for (int i = 0; i < slen; i++)
        str[i] = transferchar(str[i]);
    printf("%s", str);
    return 0;
}

全部评论

相关推荐

我是小红是我:学校换成中南
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务