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

字符串合并处理

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;
}

全部评论

相关推荐

机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
你的秋招第一场笔试是哪家
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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