将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。
这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘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”
样例输入:
dec fab
样例输出:
5D37BF
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void sort( string& word, int old );
void ProcessString( string& word );
int main( void )
{
string word1, word2;
cin >> word1 >> word2;
word1.append( word2 );
sort( word1, 0 );
sort( word1, 1 );
ProcessString( word1 );
cout << word1 << endl;
return 0;
}
void sort( string& word, int old )
{
char tmp;
int nMax;
for( nMax = old; nMax < word.length(); nMax += 2 )
;
nMax -= 2;
for( int i = nMax; i > old; i -= 2 ){
bool flag = false;
for( int j = old; j < i; j += 2 ){
if( word[j] > word[j + 2] ){
tmp = word[j];
word[j] = word[j + 2];
word[j + 2] = tmp;
flag = true;
}
}
if( !flag )
break;
}
}
void ProcessString( string& word )
{
char ch1, ch2, bit, bFlag;
for( string::iterator it = word.begin();
it != word.end(); ++it ){
ch2 = 0;
bit = 1;
bFlag = 1;
if( isdigit( *it ) ){
ch1 = *it - '0';
}else if( *it >= 'a' && *it <= 'f' ){
ch1 = *it - 'a' + 10;
}else if( *it >= 'A' && *it <= 'F' ){
ch1 = *it - 'A' + 10;
}else{
bFlag = 0;
}
if( bFlag ){
ch2 |= ( ch1 & ( 1 << 0 ) ) << 3;
ch2 |= ( ch1 & ( 1 << 1 ) ) << 1;
ch2 |= ( ch1 & ( 1 << 2 ) ) >> 1;
ch2 |= ( ch1 & ( 1 << 3 ) ) >> 3;
if( ch2 < 10 ){
*it = '0' + ch2;
}else{
*it = 'A' + ch2 - 10;
}
}
}
}