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

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/thecentry。 https://blog.csdn.net/thecentry/article/details/82527940

题目描述

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

详细描述:

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

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

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

猜你喜欢

转载自blog.csdn.net/thecentry/article/details/82527940