题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
#include <iostream>
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
char change(char c){
int x;
if(c >= 'A' && c <= 'F'){
x = c - 'A' + 10;
}else if(c >= 'a' && c <= 'f'){
x = c - 'a' + 10;
}else if(c >= '0' && c <= '9') {
x = c - '0';
}else{
return c;
}
int res = 0;
// 把第一位移到最后一位, 第二位到倒数第二位, 等等
for(int i = 0; i <= 3; i ++){
int t = (x >> (3 - i)) & 1;
res |= (t << i);
}
// 输出16进制
if(res >= 10){
return (res - 10) + 'A';
}else{
return res + '0';
}
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
string line;
getline(cin, line);
string a;
string b;
stringstream ss(line);
ss >> a >> b;
string str = a + b;
vector<char> even;
vector<char> odd;
// 添加到奇数偶数数组并排序
for(int i = 0; i < str.length(); i++){
if(i & 1){
odd.push_back(str[i]);
}else{
even.push_back(str[i]);
}
}
sort(even.begin(), even.end());
sort(odd.begin(), odd.end());
// 对于每一个字符, 进行倒序
for(int i = 0; i < str.length(); i++){
if(i & 1){
str[i] = change(odd[i/2]);
}else{
str[i] = change(even[i/2]);
}
}
cout << str << endl;
}
// 64 位输出请用 printf("%lld")