详细描述:
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。
这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘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<algorithm>
using namespace std;
int main(){
string s1, s2;
char sinChar[100], douChar[100];
char arr[16] = {'0','8','4','C','2','A','6','E','1','9','5','D','3','B','7','F'};
cin>>s1>>s2;
string s = s1 + s2;
//cout<<s<<endl;
int len = s.length();
int jishu = len/2 + len%2, oushu = len/2;
for(int i = 0; i < jishu; i++)
sinChar[i] = s[2*i];
for(int i = 0; i < oushu; i++)
douChar[i] = s[2*i+1];
sort(sinChar, sinChar + jishu);
sort(douChar, douChar + oushu);
/*
for(int i = 0; i < jishu; i++)
cout<<sinChar[i];
cout<<endl;
for(int i = 0; i < oushu; i++)
cout<<douChar[i];
cout<<endl;
*/
for(int i = 0; i < jishu; i++){
char cc = sinChar[i];
if(cc >= '0' && cc <= '9') sinChar[i] = arr[cc - '0'];
else if(cc >= 'a' && cc<= 'f') sinChar[i] = arr[ cc - 'a' + 10];
else if(cc >= 'A' && cc<= 'F') sinChar[i] = arr[ cc - 'A' + 10];
}
for(int i = 0; i < oushu; i++){
char cc = douChar[i];
if(cc >= '0' && cc <= '9') douChar[i] = arr[cc - '0'];
else if(cc >= 'a' && cc<= 'f') douChar[i] = arr[ cc - 'a' + 10];
else if(cc >= 'A' && cc<= 'F') douChar[i] = arr[ cc - 'A' + 10];
}
for(int i = 0; i < oushu; i++)
cout<<sinChar[i]<<douChar[i];
if(len%2 == 1) cout<<sinChar[jishu-1];
cout<<endl;
return 0;
}
#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; //给char 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;
}
}
}
}