字符串合并并处理
来源:互联网 发布:中科数据恢复 编辑:程序博客网 时间:2024/05/16 06:49
详细描述:
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。
这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘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赋初值为0bit = 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;}}}}
0 0
- 字符串合并并处理
- 字符串合并并处理(C++实现)
- 字符串合并处理
- 字符串合并处理
- 字符串合并处理
- 字符串合并处理 (中级)
- 字符串合并处理
- OJ-----字符串合并处理
- 字符串合并处理
- 字符串合并处理
- 字符串合并处理
- 字符串合并处理
- 各种字符串合并处理示例.
- 华为OJ_2126_字符串合并处理
- 华为oj 字符串合并处理
- 华为oj字符串合并处理
- 【华为 OJ 】 字符串合并处理
- [编程题]字符串合并处理
- ffmpeg视频相关名词解释
- opencontrail编译contrail-web-core时需要安装的外部工具nodejs
- instancetype和id的区别
- 百度2014研发类校园招聘笔试题解答
- redis集群(主从配置)
- 字符串合并并处理
- 计蒜客 难题题库 223 字母排序
- 使用 Xcode 工具(Network Link Conditioner)模拟网络状态
- android解析xml的方式
- 非整除序列
- [J2EE]jsp项目中使用UEditor富文本编辑器
- MySQL日期数据类型、时间类型使用总结
- IOS界面适配二: Auto Layout的使用(上)
- 安装ndoutils 提示“MySQL library could not be located”错误!