华为笔试题--字符串合并处理

题目描述

按照指定规则对输入的字符串进行处理。

详细描述:

将输入的两个字符串合并。

对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。

对排序后的字符串进行操作,如果字符为‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,则对他们所代表的16进制的数进行BIT倒序的操作,并转换为相应的大写字符。如字符为‘4’,为0100b,则翻转后为0010b,也就是2。转换后的字符为‘2’; 如字符为‘7’,为0111b,则翻转后为1110b,也就是e。转换后的字符为大写‘E’。

举例:输入str1为"dec",str2为"fab",合并为“decfab”,分别对“dca”和“efb”进行排序,排序后为“abcedf”,转换后为“5D37BF”

接口设计及说明:

/*

功能:字符串处理

输入:两个字符串,需要异常处理

输出:合并处理后的字符串,具体要求参考文档

返回:无

*/

void ProcessString(char* str1,char *str2,char * strOutput)

{

}

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <map>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     map<char, char> m = {
10         {'0', '0'}, {'1', '8'}, {'2', '4'}, {'3', 'C'},
11         {'4', '2'}, {'5', 'A'}, {'6', '6'}, {'7', 'E'},
12         {'8', '1'}, {'9', '9'}, {'a', '5'}, {'b', 'D'},
13         {'c', '3'}, {'d', 'B'}, {'e', '7'}, {'f', 'F'},
14         {'A', '5'}, {'B', 'D'}, {'C', '3'}, {'D', 'B'}, {'E', '7'}, {'F', 'F'}
15     };
16     string str1, str2;
17     while(cin >> str1 >> str2) {
18         string str = str1 + str2;
19         str1.clear();
20         str2.clear();
21         for(int i = 0; i < str.size(); ++i) {
22             if(i % 2) str2 += str[i];
23             else str1 += str[i];
24         } 
25         sort(str1.begin(), str1.end());
26         sort(str2.begin(), str2.end());
27         //cout << str1 << " " << str2 << endl;       
28         for(int i = 0, j = 0, k = 0; i < str.size(); ++i) {
29             if(i % 2) str[i] = str2[k++];
30             else str[i] = str1[j++];
31         }
32         //cout << str << endl;
33         for(int i = 0; i < str.size(); ++i) {
34             if(m.count(str[i]) == 0) cout << str[i];
35             else cout << m[str[i]];
36         }
37         cout << endl;
38     }
39 }

猜你喜欢

转载自www.cnblogs.com/joker1937/p/10664252.html