(0903)2013华为校园招聘机试题

本文详细介绍了三个编程题目的解答:字符串过滤去除重复字符、字符串压缩减少冗余、以及计算输入表达式的数值结果。每个部分都提供了具体的代码实现和示例解释,帮助读者理解算法逻辑和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【引言】

       jianjun说浙大那边华为已经开始招聘了,心里一惊,他说了大概几道上机的题目,我网上一搜,按照自己的思路写下来,花了肯定不止一个小时,汗啊,要是实际上机估计就答不完了。

【题目】【代码实现】

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

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

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

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

示例 
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”
*/
#include <stdlib.h>
#include <string.h>
//过滤函数
void stringFilter(const char *pInputStr, char *pOutputStr)
{

	int a[26] = {0};
	const char* pstr = pInputStr;
	char* pResult = pOutputStr;
	while( *pstr != '\0')
	{
		a[*pstr - 'a']++;
		if(a[*pstr - 'a'] > 1)
		{
			pstr++;
		}
		else
		{
			*pResult = *pstr;
			pstr++;
			pResult++;
		}			

	}
	*pResult = '\0';

}

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”

//压缩字符串
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{

	const char* pFirst = pInputStr;
	const char* pSecond = pInputStr+1;
	char* pResult = pOutputStr;
	int count = 0;
	while(*pSecond != '\0')
	{
		
		if(*pSecond == *pFirst)
		{
			count++;
		}			
		else
		{
			if(count > 0)
			{
				*pResult++ = count+1 + '0';
			}				
			*pResult++ = *pFirst;
			count = 0;
		}
		pFirst++;
		pSecond++;
	}
	if(count > 0)
	{
		*pResult++ = count+1 + '0';
	}		
	*pResult++ = *pFirst;
	*pResult = '\0';

}

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” 注:格式错误
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	char* pTemp = (char*)malloc(lInputLen);	
	char* pLeft = pTemp;
	const char* pRight = pInputStr;
	while(*pInputStr != '\0')
	{
		if(*pInputStr == '+' || *pInputStr == '-')
		{
			pRight = pInputStr + 1;
			break;
		}
		else
		{
			*pTemp++ = *pInputStr++;			
		}
	}
	*pTemp = '\0';
	if (pRight == pLeft || *pRight == '+' || *pRight == '-')
	{
		*pOutputStr++='0';
		*pOutputStr = '\0';
		return;
	}
	int L = atoi(pLeft);
	int R = atoi(pRight);
	int result;
	switch (*pInputStr)
	{
	case '+':
		result = L + R;
		break;
	case '-':
		result = L - R;
		break;
	default:
		result = 0;
		break;
	}
	itoa(result, pOutputStr, 10);		

}

【测试】

int main()
{
	char a[] = "youyouare";
	char a1[] = "deefd";
	char a2[] = "afafafaf";
	char a3[] = "ppppp";
	char* b = (char*)malloc(100);
	char* b1 = (char*)malloc(100);
	char* b2 = (char*)malloc(100);
	char* b3 = (char*)malloc(100);
	
	stringFilter(a,b);
	stringFilter(a1,b1);
	stringFilter(a2,b2);
	stringFilter(a3,b3);

	char t1[] = "cccddecc";
	char t2[] = "deefd";
	char t3[] = "pppppp";
	char rt1[15], rt2[15], rt3[15];
	stringZip(t1,0,rt1);
	stringZip(t2,0,rt2);
	stringZip(t3,0,rt3);

	char s1[] = "4+7";
	char s2[] = "4-7";
	char s3[] = "9++7";
	char st1[15], st2[15], st3[15];	
	arithmetic(s1, strlen(s1), st1);
	arithmetic(s2, strlen(s2), st2);
	arithmetic(s3, strlen(s3), st3);


}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值