题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
#include <cctype> #include <iostream> #include <algorithm> #include <string> using namespace std; string combineStrings(string& str1, string& str2) { return str1 + str2; } string sortString(string& str) { string substr1, substr2; for (int i = 0; i < str.size(); i++) { if(i%2 == 0) substr1.push_back(str[i]); else substr2.push_back(str[i]); } sort(substr1.begin(), substr1.end()); sort(substr2.begin(), substr2.end()); string sortStr; int index1 = 0, index2 = 0; for(int i = 0; i < str.size(); i++) { if(i%2 == 0) sortStr += substr1[index1++]; else sortStr += substr2[index2++]; } return sortStr; } char hexToBinaryReverse(char ch) { int value; if(isdigit(ch)) { value = ch-'0'; } else { if (islower(ch)) { ch = toupper(ch); } value = ch-'A'+10; } //将二进制数倒叙 string binary; for(int i = 0; i < 4; i++) { binary += (value & (1 << i)) ? '1' : '0'; } //倒叙后的十六进制数 int reverseValue = 0; for(int i = 0; i < 4; i++) { reverseValue = (reverseValue << 1) | (binary[i]-'0'); } if(reverseValue < 10) return reverseValue+'0'; else return reverseValue-10+'A'; } string resultString(string& str) { string resStr; for(int i = 0; i < str.size(); i++) { if((str[i] >= 'G' and str[i] <= 'Z') || (str[i] >= 'g' and str[i] <= 'z')) resStr += str[i]; else resStr += hexToBinaryReverse(str[i]); } return resStr; } int main() { string str1, str2; cin >> str1 >> str2; string combineStr = combineStrings(str1, str2); string sortStr = sortString(combineStr); string resultStr = resultString(sortStr); cout << resultStr << endl; return 0; } // 64 位输出请用 printf("%lld")
1.字符串合并
2.字符串排序
3.对0~9、a~f、A~F进行二进制倒叙输出