2014年华为机试试题


一、题目描述(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打印输出
当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响。
 

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

三、题目描述(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” 注:格式错误

// 华为2014机试.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

void stringFilter(const char *pInputstr, int length, char *outputStr)
{
	char hash[26] = {0};
	int j = 0;
	for(int i = 0;i<length;i++)
	{
		if(hash[*(pInputstr+i) - 'a'] == 0)//说明该字符是第一次出现在字符串中
		{
			*(outputStr +j) =*(pInputstr+i);
			hash[*(pInputstr+i) - 'a'] = 1;
			j++;
		}
	}
	*(outputStr + j) = '\0';
}
void  func(char *pInputStr, int length, char *outputstr)
{

	int num1  = 0, num2 = 0;
	int numofblank = 0;
	int result = 0;
	char flag = 0;
	for(int i = 0;i<length;i++)
	{
		if(*(pInputStr + i ) != ' ' && (numofblank ==0))
		{
			num1 = num1*10 +*(pInputStr+i)-'0';
		}
		if(*(pInputStr + i ) != ' ' && (numofblank ==1))
		{
			num2 = num2*10 +*(pInputStr+i)-'0';
		}
		if(*(pInputStr + i ) == ' ' && (numofblank == 0))
		{
			numofblank = 1;
			i++;
			flag = *(pInputStr + i );

		}
	}
	if(flag == '+') result = num1 + num2;
	else if(flag == '-') result = num1 - num2;
	itoa(result, outputstr, 10);;
}
void stringZip(char *pInputStr, int length, char *outputStr)
{
	int hash[26] = {0};
	int j = 0;
	for(int i = 0;i<length;i++)
	{
		if(hash[*(pInputStr+i) - 'a'] == 0)
		{
			hash[*(pInputStr+i) - 'a'] = 1;
			*(outputStr+j) = *(pInputStr+i);
			j++;
		}
		else if(*(pInputStr+i)!=*(pInputStr+i-1))
		{
			*(outputStr+j) = *(pInputStr+i);
			j++;
		}
		else if(*(pInputStr+i)==*(pInputStr+i-1))
		{
			hash[*(pInputStr+i) - 'a']++;
			*(outputStr+j) = hash[*(pInputStr+i)-'a']+'0';
			if(*(pInputStr+i)!=*(pInputStr+i+1))
				j++;
		}
	}
	outputStr[j]='\0';
}
int _tmain(int argc, _TCHAR* argv[])
{
	char inputstr1[30],inputstr2[30],inputstr3[30];
	char outputstr1[30],outputstr2[30],outputstr3[30];
	cin>>inputstr1;
	stringFilter(inputstr1,strlen(inputstr1),outputstr1);
	cout<<"the first result is "<<outputstr1;
	cin>>inputstr2;
	stringZip(inputstr2,strlen(inputstr2),outputstr2);
	cout<<"the second result is "<<outputstr2;
	cin>>inputstr2;
	func(inputstr3,strlen(inputstr3),outputstr3);
	cout<<"the third result is "<<outputstr3;
	//cout<<outputstr;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值