2014届华为校园招聘机试题

本文介绍三种字符串处理技巧:去除重复字符、字符串压缩及算术表达式求值。通过具体实例展示了如何利用哈希表和计数方法高效实现这些功能。

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

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

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

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

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

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

main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出

当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响。

代码:

#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
#include<assert.h>  
#define MAX 100  
void stringFilter(const char *pInputStr,long Inputlen,char *pOutputStr)  
{   
    int hashtable[255]={0};  
	int j=0;
	for(int i=0;i<Inputlen;i++)
	{
		if(hashtable[pInputStr[i]]==0)
		{
			hashtable[pInputStr[i]]=1;
			pOutputStr[j]=pInputStr[i];
			j++;
		}
	}
    pOutputStr[j]='\0';  
}  
int main(void)  
{  
    char *inputstr=(char *)malloc(sizeof(char )*MAX);  
    char *outputstr=(char *)malloc(sizeof(char)*MAX);  
    int len;  
    assert(inputstr!=NULL && outputstr!=NULL);  
    while(scanf("%s",inputstr)!=EOF)  
    {  
        len=strlen(inputstr);  
        stringFilter(inputstr,len,outputstr);  
        puts(outputstr);  
    }  
    return 0;  
}

二、题目描述(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”

代码:

<span style="font-size:12px;">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#define MAX 100

void stringZip(const char *pInputStr,long InputLen,char *pOutputStr)
{
	int k=0;
	int i,j;
	int num;
	for(i=0;i<InputLen;	i=i+num)
	{
		num=0;
		for(j=i;j<InputLen;j++)//找到连续的字符的个数
		{
			if(pInputStr[i]==pInputStr[j])
			{
				num++;
			}
			else
				break;
		}
		if(num!=1)//如果num>1,需要将个数放到outputstr中,
		{
		   pOutputStr[k]=(char)(num+'0'-0);
		   pOutputStr[k+1]=pInputStr[i];
		   k=k+2;
		}
		else   //如果num=1,就不用将个数1放入inputstr中了
		{
			pOutputStr[k]=pInputStr[i];
			k++;
		}
	}
	pOutputStr[k]='\0';//勿忘
}
int main(void)
{
	char *inputstr=malloc(sizeof(char )*MAX);
	char *outputstr=malloc(sizeof(char)*MAX);
	int len;
	assert(inputstr!=NULL && outputstr!=NULL);
	while(scanf("%s",inputstr)!=EOF)
	{
	    len=strlen(inputstr);
        stringZip(inputstr,len,outputstr);
	    puts(outputstr);
	}
	return 0;
}
</span>

三、题目描述(50分): 
通过键盘输入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” 注:格式错误

代码:

<span style="font-size:12px;">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#define MAX 100
void arthmetic(const char *pInputStr,long InputLen,char *pOutputStr)
{
	int i;
	int k1=0;
	char operator_str1[4];//存放第一个操作数字符串
	char operator_str2[4];//存放第二个操作数字符串
	int index1;//两个索引
	int index2;
	int num1,num2;//第一个操作数转化的整数num1,第二个操作数转化的整数num2
	for(i=0;i<InputLen;i++)
	{
		if(pInputStr[i]=='+' || pInputStr[i]=='-')
			break;
	}
	if(i == InputLen)//如果字符串中根本没有字符‘+’或'-',返回'0'
	{
		pOutputStr[0]='0';
		pOutputStr[1]='\0';
		return ;
	}
	//否则
	k1=0;
	if(pInputStr[i-1]==' '&& pInputStr[i+1]==' ')//如果两边都是空格,往下继续进行------
	{
	     for(index1=0;index1<i-1;++index1)//从开头到操作符左边的空格之前
	     {
		     if(pInputStr[index1]<'0' || pInputStr[index1]>'9')//若出现非数字字符,直接退出
		     {
			    pOutputStr[0]='0';
		        pOutputStr[1]='\0';
		        return ;
		      }
            //否则
		    operator_str1[k1]=pInputStr[index1];
		     k1++;	
	     }
	    operator_str1[k1]='\0';

	    k1=0;
	    for(index2=i+2;index2<InputLen;++index2)//从操作符右边的空格后,到字符串结束
	    {
		    if(pInputStr[index2]<'0' || pInputStr[index2]>'9')//若出现非数字字符,直接退出
		    {
			   pOutputStr[0]='0';
		       pOutputStr[1]='\0';
		       return ;
		    }
			//否则
		    operator_str2[k1]=pInputStr[index2];
		    k1++;
	    }
	    operator_str1[k1]='\0';

	    num1=atoi(operator_str1);
	    num2=atoi(operator_str2);
	    if(num1>=100 || num2>=100)//若两个整数都大于100,直接输出错误标志
	    {
		   pOutputStr[0]='0';
		   pOutputStr[1]='\0';
		   return ;
	    }
	    else //否则继续处理
	    {
		   if(pInputStr[i]=='+')
              itoa(num1+num2,pOutputStr,10);
		   else
		      itoa(num1-num2,pOutputStr,10);
	    }
     }
	else//否则出错格式错误标志"0"--------------------------------------
	{
		pOutputStr[0]='0';
		pOutputStr[1]='\0';
		return ;
	}
}
int main(void)
{
	char *inputstr=malloc(sizeof(char )*MAX);
	char *outputstr=malloc(sizeof(char)*MAX);
	int len;
	assert(inputstr!=NULL && outputstr!=NULL);
	gets(inputstr);
	len=strlen(inputstr);
    arthmetic(inputstr,len,outputstr);
	puts(outputstr);
	free(inputstr);
	free(outputstr);
	inputstr=outputstr=NULL;
	return 0;
}</span>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值