华为几则机试题

时间:2012-09-15   地点:电子科大

上机时间两小时,3道题

1 字串转换
问题描述:
将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
要求实现函数:
int convert(char *input,char* output)
【输入】  char *input , 输入的字符串
【输出】  char *output ,输出的字符串
【返回】 无
示例
输入:char*input="abcd" 
输出:char*output="bcde"
输入:char*input="abbbcd" 
输出:char*output="bcdcde"


2 字符串处理转换

问题描述:    
在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:
void my_word(charinput[], char output[])
【输入】  char input[], 输入的字符串
【输出】  char output[],输出的字符串
【返回】 无
示例
输入:charinput[]="some local buses, some1234123drivers" ,
输出:charoutput[]="drivers local buses some"
输入:charinput[]="%A^123 t 3453i*()" ,

输出:charoutput[]=""


3 正数减法
问题描述:    
两个任意长度的正数相减,这两个正数可以带小数点,也可以是整数,请输出结果。 输入的字符串中,不会出现除了数字与小数点以外的其它字符,不会出现多个小数点以及小数点在第一个字符的位置等非法情况,所以考生的程序中无须考虑输入的数值字符串非法的情况。 
详细要求以及约束:
1.输入均为正数,但输出可能为负数; 
2.输入输出均为字符串形式;
3.如果输出是正数则不需要带符号,如果为负数,则输出的结果字符串需要带负号
例如:2.2-1.1 直接输出为“1.1”,1.1-2.2 则需要输出为“-1.1”
 4.输出的结果字符串需要过滤掉整数位前以及小数位后无效的0,小数位为全0的,直接输出整数位
例如相减结果为11.345,此数值前后均不可以带0,“011.345”或者“0011.34500”等等前后带无效0的均视为错误 输出。例如1.1-1.1结果为0.0,则直接输出0。
要求实现函数:
void Decrease(char *input1, char*input2, char *output)
【输入】 char *iinput1 被减数
char*nput2 减数 
【输出】 char *output 减法结果
【返回】 无
示例
输入:char *input1="2.2" 
char *input2="1.1"
输出:char*output="1.1"
输入:char *input1="1.1" 
char *input2="2.2"

输出:char *output="-1.1"

第一道题,由于被后来的代码覆盖了,也鉴于第一道题目比较简单,所以没有展示。

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

void my_word(char input[], char output[]);
void Decrease(const char *input1, const char*input2, char *output);

int main()
{
#if 0
	char srcArray[] = "some local buses, some1234123drivers";
	char destArray[64];

	memset(destArray,0,sizeof(char)*64);
	
	my_word(srcArray,destArray);

	for (int i = 0; i < 64; i++)
	{
		if (destArray != 0)
			cout<<destArray[i];
		else 
			break;
	}
	cout<<endl;
#endif

	char input1[] = "123456789123456789";
	char input2[] = "0";
	char resutl[21];
	memset(resutl,0,sizeof(char)*21);

	Decrease(input1,input2,resutl);

	for (int i = 0; i < strlen(input1); i++)
	{
		cout<<input1[i];
	}
	cout<<" - ";

	for (int i = 0; i < strlen(input2); i++)
	{
		cout<<input2[i];
	}
	cout<<" = " ;

	for (int i = 0; i < 64; i++)
	{
		if (resutl[i] != 0)
			cout<<resutl[i];
		else 
			break;
	}
	cout<<endl;
	getchar();
}

bool VectorCmp(const vector<char>& left,const vector<char>&right)
{
	return left.size() > right.size();
}

void my_word(char input[], char output[])
{
	unsigned int len = strlen(input);

	unsigned int start = 0;
	unsigned int end = 0;

	vector<vector<char> >words;
	
	while(start < len)
	{
		// skip the alpha 
		while ( isalpha(input[start]))
		{
			start++;
		}

		// skip the non-alpha 
		if (start - end < 1) 
		{
			while(!isalpha(input[start])) start++;

			end = start;
		}
		else
		{
			vector<char> word;
			for (int i = end; i < start; ++i)
			{
				word.push_back(input[i]);
			}

			words.push_back(word);

			start++;
			end = start;
		}
	}

	stable_sort(words.begin(),words.end(),VectorCmp);
	
	vector<vector<char> >::iterator it = unique(words.begin(),words.end());
	words.erase(it,words.end());

	unsigned int index = 0;
	for (vector<vector<char> >::iterator it = words.begin(); it != words.end(); ++it)
	{
		vector<char>::iterator iit = it->begin();
		while(iit != it->end())
		{
			output[index++] = *iit;
			++iit;
		}
		output[index++] = ' ';
	}
}

void Decrease( const char *input1,const char*input2, char *output)
{
	unsigned int len1 = strlen(input1);
	unsigned int len2 = strlen(input2);
	unsigned int dot1 = 0;
	unsigned int dot2 = 0;
	bool bDot = false;

	char* leftDotNum1 = new char[len1 + 1];
	char* rightDotNum1 = new char[len1 + 1];

	memset(leftDotNum1,0,sizeof(char)*(len1 + 1));
	memset(rightDotNum1,0,sizeof(char)*(len1 + 1));

	// part the number as integer and decimal
	for (int i = 0; i < len1; ++i)
	{
		if (input1[i] != '.' && !dot1)
		{
			leftDotNum1[i] = input1[i];
		}
		else if (dot1 && input1[i] != '.')
		{
			rightDotNum1[i-dot1 - 1] = input1[i];
		}
		else
		{
			dot1 = i;
			if (!bDot) bDot = true;
		}
	}

	char* leftDotNum2 = new char[len2 + 1];
	char* rightDotNum2 = new char[len2 + 1];

	memset(leftDotNum2,0,sizeof(char)*( len2 + 1));
	memset(rightDotNum2,0,sizeof(char)*(len2 + 1 ));

	for (int i = 0; i < len2; ++i)
	{
		if (input2[i] != '.' && !dot2)
		{
			leftDotNum2[i] = input2[i];
		}
		else if (input2[i] != '.' && dot2)
		{
			rightDotNum2[i - dot2 - 1] = input2[i];
		}
		else
		{
			dot2 = i;
			if (!bDot) bDot = true;
		}
	}

	//cout<<leftDotNum1<<" : "<<(strlen(rightDotNum1) ? rightDotNum1 : "")<<endl;
	//cout<<leftDotNum2<<" : "<<(strlen(rightDotNum2) ? rightDotNum2 : "")<<endl;

	len1 = max(strlen(leftDotNum1),strlen(leftDotNum2));
	len2 = max(strlen(rightDotNum1),strlen(rightDotNum2));

	// test whether the result is negative number
	bool bNegative = false;
	if (strlen(leftDotNum1) < strlen(leftDotNum2)) bNegative = true;
	else if (strlen(leftDotNum1) == strlen(leftDotNum2))
	{
		for (int i = 0; i <len1 ; i++)
		{
			if (leftDotNum1[i] < leftDotNum2[i])
			{
				bNegative = true;
				break;
			}
			else if (leftDotNum1[i] > leftDotNum2[i]) 
				break;
		}
	}

	if (!bNegative && !strcmp(leftDotNum1,leftDotNum2))
	{
		for (int i = 0; i < len2; i++)
		{
			if ( i < strlen(rightDotNum1) && i < strlen(rightDotNum2) &&rightDotNum1[i] < rightDotNum2[i])
			{
				bNegative = true;
				break;
			}
			else if (i < strlen(rightDotNum2) && i > strlen(rightDotNum1))
			{
				bNegative = true;
				break;
			}
		}
	}

	bool* leftAuxArray = new bool[len1];
	bool* rightAuxArray = new bool[len2];

	memset(leftAuxArray,0,sizeof(bool)*len1);
	memset(rightAuxArray,0,sizeof(bool)*len2);

	char* leftResult = new char[len1 + 1];
	char* rightResult = new char[len2 + 1];
	memset(leftResult,0,sizeof(char)*(len1 + 1));
	memset(rightResult,0,sizeof(char)*(len2 + 1));
	
	//deal with the number after the dot
	char* pLeftNum = rightDotNum1;
	char* pRightNum = rightDotNum2;
	if (bNegative)
	{
		pLeftNum = rightDotNum2;
		pRightNum = rightDotNum1;
	}
	unsigned int leftLen = strlen(pLeftNum);
	unsigned int rightLen = strlen(pRightNum);

	for (int  index = len2 - 1; index >= 0; index--)
	{
		int result;
		if (leftLen > index && rightLen > index)
		{
			result = pLeftNum[index] - pRightNum[index];
			if (index != len2 - 1 && rightAuxArray[index + 1]) 
				result -= 1;

			if (result < 0)
			{
				result += 10;
				rightAuxArray[index] = true;
			}
			rightResult[index] = result + 0x30;
		}
		else if (rightLen <= index)
		{
			rightResult[index] = pLeftNum[index];
		}
		else if (leftLen <= index)
		{
			result = 10 - (pRightNum[index] - 0x30);
			rightAuxArray[index] = true;
			rightResult[index] = result + 0x30;
		}
	}

	//cout<<"Right: ";
	//for(int i = 0; i < strlen(rightResult);i++) cout<<rightResult[i];
	//cout<<endl;

	// deal with the number before dot
	pLeftNum = leftDotNum1;
	pRightNum = leftDotNum2;
	if (bNegative)
	{
		pLeftNum = leftDotNum2;
		pRightNum = leftDotNum1;
	}

	leftLen = strlen(pLeftNum);
	rightLen = strlen(pRightNum);
	bool bfirstNum = true;
	unsigned int step = 1;
	while( step <= leftLen && step <= rightLen)
	{
		int result = pLeftNum[leftLen - step] - pRightNum[rightLen - step];
		
		if (bfirstNum && bDot && rightAuxArray[0]) result -= 1;
		else if (!bfirstNum && leftAuxArray[len1 - step + 1])
			result -= 1;

		if (result < 0) 
		{
			result += 10;
			leftAuxArray[len1 - step] = true;
		}
		leftResult[len1 - step] = result + 0x30;
		bfirstNum = false;
		step++;
	}
	while (step <= leftLen)
	{
		int result = pLeftNum[leftLen - step] - 0x30;
		if (leftAuxArray[len1 - step + 1]) result -= 1;

		if (result < 0) 
		{
			result += 10;
			leftAuxArray[len1 - step] = true;
		}

		leftResult[len1 - step] = result + 0x30;
		step++;
	}

	unsigned int curInd = 0;
	if (bNegative)
	{
		output[0] = '-';
		curInd += 1;
	}
	//cout<<"OK3\n";
	//cout<<"Left: ";
	//for(int i = 0; i < strlen(leftResult);i++) cout<<leftResult[i];
	//cout<<endl;

	bool start = false;
	bool bempty = true;
	for (int i = 0; i < strlen(leftResult); i++)
	{
		if (!start && leftResult[i] == 0x30) continue;
		else
		{
			start = true;
			output[curInd++] = leftResult[i];
			bempty = false;
		}
	}

	if (strlen(rightResult))
	{
		int endInd = strlen(rightResult) - 1;
		
		while (endInd >= 0)
		{
			if (rightResult[endInd] == 0x30) endInd--;
			else break;
		}

		if (endInd >= 0) 
		{
			if (bempty)
			{
				output[curInd++] = '0';
			}
			output[curInd++] = '.';
			bempty = false;

			for (int i = 0; i <= endInd; i++)
			{
				output[curInd++] = rightResult[i];
			}
		}	
	}
	if (bempty)
	{
		output[curInd++] = '0';
	}
	output[curInd] = 0;

	delete []leftDotNum1;
	delete []leftDotNum2;
	delete []rightDotNum1;
	delete []rightDotNum2;
	delete []leftAuxArray;
	delete []rightAuxArray;
}

1、 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数: 
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
         lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”
*/


2、 /*
题目描述(40分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"

要求实现函数: 
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:   输入字符串
              lInputLen:   输入字符串长度             
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“cccddecc”    输出:“3c2de2c”
输入:“adef”       输出:“adef”
输入:“pppppppp” 输出:“8p”

3、
通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:
1. 操作数为正整数,不需要考虑计算结果溢出的情况。
2. 若输入算式格式错误,输出结果为“0”。

要求实现函数: 
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:   输入字符串
              lInputLen:   输入字符串长度             
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“4 + 7”   输出:“11”
输入:“4 - 7”   输出:“-3”
输入:“9 ++ 7”   输出:“0” 注:格式错误

#include <iostream>
#include <sstream>
using namespace std;

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

const int SIZE = 21;

int main()
{
#if 0
	char srcArray[] = "adef";
	char destArray[SIZE] = {0};

	stringZip(srcArray,strlen(srcArray),destArray);

	for (int i = 0; i < strlen(destArray); i++)
	{
		cout<<destArray[i];
	}
	cout<<endl;
#endif
	char srcArray[] = "9 ++ 7";
	char destArray[SIZE] = {0};

	arithmetic(srcArray,strlen(srcArray),destArray);

	for (int i = 0; i < strlen(destArray); i++)
	{
		cout<<destArray[i];
	}
	cout<<endl;

	getchar();
}

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	const char* pFirst = pInputStr;
	const char* pSecond = pFirst + 1;
	char* pResutl = pOutputStr;
	int count = 1;

	while (*pSecond)
	{
		if (*pSecond == *pFirst)
		{
			count++;
		}
		else
		{
			*pResutl++ = count + 0x30;
			*pResutl++ = *pFirst;
			count = 1;
		}
		pFirst++;
		pSecond++;
	}
	*pResutl++ = count + 0x30;
	*pResutl++ = *pFirst;
	pResutl = 0;
}

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	istringstream istr(pInputStr);
	string data1;
	string data2;
	string op;

	istr>>data1;
	istr>>op;
	istr>>data2;

	int leftData = atoi(data1.c_str());
	int rightData = atoi(data2.c_str());
	int bValid = op.length() == 1;

	if (!bValid) 
	{
		cout<<"Not valid!\n";
		exit(1);
	}

	int result = 0;
	switch(*(op.c_str()))
	{
	case '+':
		result = leftData + rightData;
		break;

	case '-':
		result = leftData - rightData;
		break;

	case '*':
		result = leftData * rightData;
		break;

	case '/':
		if (rightData)
			result = leftData/rightData;
		break;
	}

	itoa(result,pOutputStr,10);
}

据说这个也是华为的机试题目,先写上再说,来自 http://topic.csdn.net/u/20110812/10/9a3a305d-edf8-4302-b341-b3fdda5bfdca.html
//示例代码:输入单词,统计单词出现次数并按照单词出现次数从多到少排序
#include <cstdlib>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
 
void sortMapByValue(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector);
int cmp(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y);
 
int main()
{
 std::map<std::string, int> tMap;  // 新建了个map
 std::string word;
 while (std::cin >> word)
 {
 std::pair<std::map<std::string, int>::iterator, bool> ret = tMap.insert(std::make_pair(word, 1));
 if (!ret.second)
 ++ret.first->second;
 } 
 
 std::vector<std::pair<std::string,int> > tVector;  // 新建了个vector
 sortMapByValue(tMap,tVector);  //函数里把map的数据copy一份到vector,然后vector排序。
                                //也就是说,现在内存里有两份数据,一份map,还是按key排的。
                                //另一份vector,按value排
 for(int i=0;i<tVector.size();i++)
 {
    std::cout<<tVector[i].first<<": "<<tVector[i].second<<std::endl;
 }  
 
 system("pause");
 return 0;
}
 
int cmp(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y)
{
 return x.second > y.second;
}
 
void sortMapByValue(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector)
{
 // 遍历map,然后把每一份数据复制进vector
 for (std::map<std::string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)
 {
 tVector.push_back(std::make_pair(curr->first, curr->second));
 }
 // 排vector,和map无关。
 std::sort(tVector.begin(), tVector.end(), cmp);
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值