<table id="table1" class="grid grid_tb " border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="grid_left_td" width="10%">描述:?</td><td width="90%"><p>按照指定規則對輸入的字符串進行處理。</p><p>詳細描述:</p><p>將輸入的兩個字符串合并。</p><p>對合并后的字符串進行排序,要求為:下標為奇數的字符和下標為偶數的字符分別從小到大排序。這里的下標意思是字符在字符串中的位置。</p><p>對排訓后的字符串進行操作,如果字符為‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,<span style="color:#FF0000">則對他們所代表的16進制的數進行BIT倒序的操作,并轉換為相應的大寫字符</span>。如字符為‘4’,為0100b,則翻轉后為0010b,也就是2。轉換后的字符為‘2’;?如字符為‘7’,為0111b,則翻轉后為1110b,也就是e。轉換后的字符為大寫‘E’。</p><p>?</p><p>舉例:輸入str1為"dec",str2為"fab",合并為“decfab”,分別對“dca”和“efb”進行排序,排序后為“abcedf”,轉換后為“5D37BF”</p><p>接口設計及說明:</p><p>/*</p><p>功能:字符串處理</p><p>輸入:兩個字符串,需要異常處理</p><p>輸出:合并處理后的字符串,具體要求參考文檔</p><p>返回:無</p><p>*/</p><p>void?ProcessString(char*?str1,char?*str2,char?*?strOutput)</p><p>{</p><p>}</p><p>?</p><p>?</p>?</td></tr><tr><td class="grid_left_td">知識點:</td><td>?字符串,排序,位運算?</td></tr><tr><td class="grid_left_td">題目來源:</td><td>?內部整理?</td></tr><tr><td class="grid_left_td">練習階段:</td><td>?中級?</td></tr><tr><td class="grid_left_td">運行時間限制:</td><td>10Sec</td></tr><tr><td class="grid_left_td">內存限制:</td><td>128MByte</td></tr><tr><td class="grid_left_td">輸入:</td><td>?<p>輸入兩個字符串</p>?</td></tr><tr><td class="grid_left_td">輸出:</td><td>?<p>輸出轉化后的結果</p>?</td></tr><tr><td class="grid_left_td">樣例輸入:</td><td><pre>dec fab
</pre></td></tr><tr><td class="grid_left_td">樣例輸出:</td><td><pre>5D37BF
</pre></td></tr></tbody></table>
**關于二進制位的翻轉問題參考:[二進制位的翻轉](http://blog.csdn.net/u013074465/article/details/45485959)**
根據“二進制位翻轉”這篇文章可以得到如下 程序將要使用的、對4位二進制位的翻轉,可以如下處理:
~~~
int main() {
unsigned int n;
cin >> n;
n = n & 0xf;
cout << hex << n << endl;
n = ((n & 0x5) << 1) | ((n & 0xa) >> 1);
n = ((n & 0x3) << 2) | ((n & 0xc) >> 2);
cout << n << endl;
}
~~~
上面的程序將形如“1011”的二進制位序列翻轉為“1101”。
解題如下:
~~~
#include <string>
//#include <algorithm>
#include <iostream>
//#include <string.h>
//#include <vector>
//#include <cmath>
#include <sstream>
using namespace std;
//變形的快排,對字符串分別排序其奇數下標字符、偶數下標字母
int PartQuickSort(string &str, int pos1, int pos2) {
char pivot = str[pos1];
int low = pos1, high = pos2;
while (high - low > 1) {
while (high - low > 1 && str[high] >= pivot)
high -= 2;
str[low] = str[high];
while (high - low > 1&& str[low] <= pivot)
low += 2;
str[high] = str[low];
}
str[low] = pivot;
return low;
}
void QuickSort(string &str, int pos1, int pos2) {
if (pos2 - pos1 > 1) {
int part = PartQuickSort(str, pos1, pos2);
QuickSort(str, pos1, part - 2);
QuickSort(str, part + 2, pos2);
}
}
//按照題目要求對特定字符翻轉其二進制位
void process_str(string &str) {
char ch1, ch2 = 0;
bool flag = true;
int i;
for (i = 0; i < str.size(); i++) {
flag = true;
if (str[i] >= '0' && str[i] <= '9')
ch1 = str[i] - '0';
else if (str[i] >= 'a' && str[i] <= 'f')
ch1 = str[i] - 'a' + 10;
else if (str[i] >= 'A' && str[i] <= 'F')
ch1 = str[i] - 'A' + 10;
else
flag = false;
if (flag) {
//本題要注意這里將二進制位翻轉的做法。
//例如11的二進制為1011,翻轉后為1101
ch2 = 0;
ch2 |= (ch1 & (1 << 0)) << 3;
ch2 |= (ch1 & (1 << 1)) << 1;
ch2 |= (ch1 & (1 << 2)) >> 1;
ch2 |= (ch1 & (1 << 3)) >> 3;
if (ch2 < 10) {
str[i] = '0' + ch2;
//cout << str[i] << endl;
}
else {
str[i] = 'A' + ch2 - 10;
//cout << str[i] << endl;
}
}
}
}
int main() {
string str, str1, str2;
getline(cin, str);
stringstream stream(str);
stream >> str1 >> str2;
str1 += str2;
str2 = str1;
//分別對字符串的偶數下標、奇數下標排序
if (str1.size() % 2 != 0) {
QuickSort(str1, 0, str1.size() - 1);
QuickSort(str1, 1, str1.size() - 2);
}
else {
QuickSort(str1, 0, str1.size() - 2);
QuickSort(str1, 1, str1.size() - 1);
}
process_str(str1);
cout << str1 << endl;
}
~~~

- 前言
- Josephus約瑟夫問題及其變種
- 鏈表的常見實現
- 二叉樹遍歷、插入、刪除等常見操作
- 二叉堆的插入刪除等操作C++實現
- 插入排序和希爾排序
- 堆排序
- 歸并排序及其空間復雜度的思考
- 快速排序的幾種常見實現及其性能對比
- 紅黑樹操作及實現
- 整數的二進制表示中1的個數
- 位操作實現加減乘除四則運算
- 冒泡排序的改進
- 直接選擇排序
- 不借助變量交換兩個數
- 基礎排序算法總結
- AVL樹(Adelson-Velskii-Landis tree)
- avl樹的C++實現
- 動態規劃之鋼條分割
- hash函數的基本知識
- 動態規劃:求最長公共子串/最長公共子序列
- 最長遞增子序列
- 稱砝碼問題
- 汽水瓶
- 字符串合并處理(二進制位的倒序)
- 動態規劃:計算字符串相似度
- m個蘋果放入n個盤子
- 生成k個小于n的互不相同的隨機數
- 棧和隊列的相互模擬
- 字符串的排列/組合
- KMP(Knuth-Morris-Pratt)算法
- n個骰子的點數
- 位運算的常見操作和題目