2014校园招聘之一(8月华为机试题)

2014校园招聘华为机试题

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

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

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

代码如下:

class Solution{
public:
	void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){
		int hash[26] = {0};
		int j = 0;
		for(int i = 0; i < lInputLen; i++){
			hash[*pInputStr - 'a'] ++;
			cout << *pInputStr << "的hash: " << hash[*pInputStr - 'a'] << endl;
			if(hash[*pInputStr - 'a'] == 1){
				pOutputStr[j++] = *pInputStr;
			}
			pInputStr++;
		}
		pOutputStr[j] = '\0';
		cout << pOutputStr << endl;
	}
};

测试代码:

int main(){
	
	const char* input = "abacacde";
	long len = strlen(input);
	char* output = (char*)malloc(sizeof(char) * (len + 1));
	Solution s;
	s.stringFilter(input, len, output);
}


2、通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串(重复次数+字母)。

函数格式:void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);


代码如下:

class Solution2{
public:
	void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){
		int j = 0;
		int cnt = 1;
		for(int i = 1; i <= lInputLen ; ){
			while(pInputStr[i] == pInputStr[i - 1]){
				i++;
				cnt++;
			}
			cout << cnt << endl;
			if(cnt == 1){
				pOutputStr[j++] = pInputStr[i - 1];
			}
			else{ //如果出现次数大于1次,有可能10以上次数,所以得处理
				int size = 0;
				char tmp[100];
				while(cnt > 0){
					tmp[size++] = cnt % 10 + '0';
					cnt /= 10;
				}
				for(int k = size - 1; k >= 0; k--){
					pOutputStr[j++] = tmp[k];
				}
				pOutputStr[j++] = pInputStr[i - 1];
				cnt = 1;
			}	
			i++;
		}
		pOutputStr[j] = '\0';
		cout << pOutputStr << endl;
	}
};

测试代码:

int main(){
	const char* input = "aaabacccacddddddddddddee";
	char* output = (char*)malloc(sizeof(char) * (strlen(input) + 1));
	Solution2 s;
	s.stringZip(input, strlen(input), output);
	
}

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

函数格式:void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);


代码如下:

<pre name="code" class="cpp">class Solution3{
public:
	void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){
		char a[100]; //保存第一个数字的字符串
		char b[100]; //保存第二个数字的字符串
		char c;
		int sum;
		int i = 0, j = 0;
		while(*pInputStr != ' ' && *pInputStr != '\0'){
			if((*pInputStr - '0' ) > 9 || (*pInputStr - '0' ) < 0){
				cout << "第一个数字输入错误" << endl;
				cout << 0 << endl;
				exit(0);
			}
			a[i++] = *pInputStr;
			pInputStr++;
		}
		a[i] = '\0';
		int aa = atoi(a);
		cout << "aa = " << aa << endl;
		pInputStr++; //遇到空格后移动到空格后的一个字符,空格后应该检查是否为正负号
		if((*pInputStr != '+') && (*pInputStr != '-')){
			cout << "+-输入错误" << endl;
			cout << 0 << endl;
			exit(0);
		}else{
			c = *pInputStr++;  //此时指针++后指向符号后面的空格
			cout << "c = " << c << endl;
		}
		if(*pInputStr++ != ' '){//正负号后,应该检查是否再有一个空格
				cout << "空格输入错误" << endl;
				cout << 0 << endl;
				exit(0);
			}
		while(*pInputStr != '\0'){
			if((*pInputStr - '0' ) > 9 || (*pInputStr - '0' ) < 0){
				cout << "第二个数字输入错误" << endl;
				cout << 0 << endl;
				exit(0);
			}
			b[j++] = *pInputStr;
			pInputStr++;
		}
		b[j] = '\0';
		int bb = atoi(b);
		cout << "bb = " << bb << endl;
		if(c == '+'){
			sum = aa + bb;
		}
		else{
			sum = aa- bb;
		}
		cout << sum << endl;
		itoa(sum, pOutputStr, 10);
		cout << "输出的字符串为: " << pOutputStr << endl;
	}
};


 

测试代码:

int main(){
	const char* input = "100 - 52";
	char output[100];
	Solution3 s;
	s.arithmetic(input, 9, output);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值