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

字符串合并处理

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

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
void Sort(string& arr, int n) {
    string temp;
    int j = 0;
    for (int i = n; i < arr.size(); i = i + 2) {
        temp = temp + arr[i];
    }
    sort(temp.begin(), temp.end());
    for (int i = n; i < arr.size(); i = i + 2) {
        arr[i] = temp[j];
        j++;
    }
}
char solvenu(int temp) {
    int nu = 1;
    string t;
    int change = 0;
    char r;
    for (int i = 0; i < 4; i++) {
        if (nu & temp) {
            t = t + '1';
        } else {
            t = t + '0';
        }
        nu = nu << 1;
    }
    int count = 3;
    for (auto a : t) {
        change = change + (a - '0') * pow(2, count);
        count--;
    }
    if (change < 10) {
        r = ('0' + change);
    } else {
        r = ('A' + (change - 10));
    }
    return r;
}

int main() {
    string str, str1, str2;
    vector<char> arr1, arr2;
    cin >> str1 >> str2;
    str = str1 + str2;
    Sort(str, 1);
    Sort(str, 0);
    string res;
    for (auto a : str) {
        if (a <= '9' && a >= '0') {
            int temp = a - '0';
            res = res + solvenu(temp);
        } else if (a <= 'f' && a >= 'a') {
            int temp = a - 'a' + 10;
            res = res + solvenu(temp);
        } else if (a <= 'F' && a >= 'A') {
            int temp = a - 'A' + 10;
            res = res + solvenu(temp);
        } else {
            res += a;
        }
    }
    cout << res;
}

全部评论

相关推荐

11-26 22:34
已编辑
重庆邮电大学 Java
快手 客户端开发 (n+5)k*16 公积金12
点赞 评论 收藏
分享
11-24 11:23
门头沟学院 C++
点赞 评论 收藏
分享
joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务