字符串合并处理/华为机试(C/C++)

题目描述

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

详细描述:

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

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

对排序后的字符串进行操作,如果字符为‘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

输入

dec fab

输出

5D37BF

代码:

//第三十题 字符串合并处理
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int Poccess(char c)
{ 
	if (isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
	{  
		if (isdigit(c))
			c = c - '0';
		else if (c >= 'A' && c <= 'F')
			c = c - 'A' + 10;
		else
			c = c - 'a' + 10;
		int iRe = 0;
		for (int i = 0; i < 4; i++)
		{
			iRe = 2 * iRe + (c & 1);
			c = c / 2;
		}
		if (iRe <= 9)
			return (iRe + '0');
		else
		{
			return (iRe - 10 + 'A');
		}
	}
	else
		return c;
	
}
int main()
{
	string inStr1;
	string inStr2;
	while (cin >> inStr1 >> inStr2)
	{
		string dStr = inStr1 + inStr2;
		string dStr1, dStr2;
		size_t iLength = dStr.length();
		for (int i = 0; i < iLength; i++)
		{
			if (i & 1)
				dStr2 += dStr[i];
			else
				dStr1 += dStr[i];
		}
		sort(dStr1.begin(), dStr1.end());
		sort(dStr2.begin(), dStr2.end());
		int k1 = 0, k2 = 0;
		for (int i = 0; i < iLength; i++)
		{
			if (i & 1)
			{
				dStr[i] = Poccess(dStr2[k2]);
				k2++;
			}	
			else
			{
				dStr[i] = Poccess(dStr1[k1]);
				k1++;
			}
		}
		cout << dStr.c_str() << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值