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

字符串合并处理

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

#include <cctype>
#include <iostream>
using namespace std;

string string_sort(string s)
{
    for (size_t i = 0; i < s.size() - 2; ++i) {
        for (size_t j = i + 2; j < s.size(); j += 2) {
            if (s[i] > s[j]) {
                auto tmp = s[i];
                s[i] = s[j];
                s[j] = tmp;
            }
        }
    }
    return s;
}

int to_dec(char c)
{
    if (isalpha(c)) {
        c = toupper(c);
        return c - 'A' + 10;
    }
    return c - '0' + 0;
}

char to_hex(int n)
{
    if (n >= 10) {
        return n - 10 + 'A';
    }
    return n + '0';
}

bool is_hex(char c)
{
    return ('0' <= c && c <= '9') 
            || ('a' <= c && c <= 'f')
            || ('A' <= c && c <= 'F');
}

string transform(string s)
{
    for (size_t i = 0; i < s.size(); ++i) {
        char c = s[i];
        if (!is_hex(c)) {
            continue;
        }
        int n = to_dec(c);
        int v = ((n & 0b1000) >> 3)
                + ((n & 0b0100) >> 1)
                + ((n & 0b0010) << 1)
                + ((n & 0b0001) << 3);
        char c2 = to_hex(v);
        s[i] = c2;
    }
    return s;
}

int main() {
    string s1, s2;
    while (cin >> s1 >> s2) {
        auto s = string_sort(s1 + s2);
        s = transform(s);
        cout << s << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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