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

字符串合并处理

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

#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;


void trans(char& a){
    if(isdigit(a) || (a >= 'A' && a <= 'F') || (a >='a' && a <= 'f')){
        int tar;
        if((a >= 'A' && a <= 'F') || (a >='a' && a <= 'f')){
            a = toupper(a);
            tar = static_cast<int>(a-'A' + 10);
        } else {
            tar = static_cast<int>(a-'0');
        }
        
        string s = bitset<4>(tar).to_string();
        reverse(s.begin(), s.end());
        bitset<4> bs(s);
        auto res = bs.to_ulong();
        
        if(res > 9){
            a = static_cast<char> ('A' + (res - 10));
        } else {
            a = static_cast<char> ('0' + res);
        }
    }
}


int main() {
    string s1, s2;
    cin >> s1 >> s2;
    string s = s1 + s2;
    // cout << s <<endl;

    int n = s.size();
    s1.clear();
    s2.clear();
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0) {
            s1 += s[i];
        } else {
            s2 += s[i];
        }
    }
    sort(s1.begin(), s1.end());
    sort(s2.begin(), s2.end());
    int loc1 = 0, loc2 = 0;
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0) {
            s[i] = s1[loc1++];
        } else {
            s[i] = s2[loc2++];
        }
    }
    // cout << s << endl;

    for(int i=0; i<n; i++){
        trans(s[i]);
    }

    cout << s << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

何 2进制 有关的题目可以多考虑 bitset 这个模版 非常好用

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务