2014 Huawei computer questions and reference answers

1. Title description (60 points):
Input a string of lowercase letters (a~z) through the keyboard. Please write a string filter program. If there are multiple identical characters in the string, the characters that are not the first occurrence will be filtered out.
For example, the filter result of the string "abacacde" is "abcde".

Required to implement the function: void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

[Input] pInputStr: Input string
            lInputLen: Input string length         
[Output] pOutputStr: Output string, the space has been opened up, and the length is the same as the input string;
 

[Note] Only need to complete the function algorithm, there is no need for any IO input and output in the middle

Example 
Input: "deefd" Output: "def"
Input: "afafafaf" Output: "af"
Input: "pppppppp" Output: "p"

The main function has been hidden, and it is reserved for the user's test entry. Test your implementation function here. You can call printf to print
the output. You can use other methods to test, as long as the final program can be executed correctly, the function implementation can be modified arbitrarily , but don't change the function prototype. Be sure to ensure that compilation and running are not affected.
 

#include <iostream>
using namespace std;

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	int hash[256] = {0};
	int j = 0;
	for(int i=0; i<lInputLen; i++){
		if(hash[pInputStr[i]] == 0){
			pOutputStr[j++] = pInputStr[i];
			hash[pInputStr[i]] = 1;
		}
	}
	pOutputStr[j] = '\0';
}
int main()
{
	char strIn[100],strOut[100];
	while(cin>>strIn){
		int len = strlen(strIn);
		stringFilter(strIn, len, strOut);
		cout<<strOut<<endl;
	}
	system("pause");
	return 0;
}


2. Title description (40 points):
Input a string of lowercase letters (a~z) through the keyboard. Please write a string compression program to compress the repeated letters in the string and output the compressed string.
Compression rules:
1. Only the characters that appear continuously and repeatedly are compressed. For example, since the string "abcbc" has no consecutive repeated characters, the compressed string is still "abcbc".
2. The format of the compressed field is "number of repetitions of characters + characters". For example: the string "xxxyyyyyyz" becomes "3x6yz" after being compressed.

Required to implement the function: 
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);

[Input] pInputStr: Input string
            lInputLen: Input string length
[Output] pOutputStr: Output string, the space has been opened up, and the length is the same as the input string;

[Note] Only need to complete the function algorithm, there is no need for any IO input and output in the middle

Example 
Input: "cccddecc" Output: "3c2de2c"
Input: "adef" Output: "adef" Input
: "pppppppp" Output: "8p"

#include <iostream>
using namespace std;

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	int i,j,k,temp;
	int pr = 0;

	for(i=0 ; i<lInputLen; i++){
		k = 1;
		temp = pInputStr[i];
		j = i+1;
		while(pInputStr[j] == temp){
			k++;
			j++;
		}
		if(k == 1){
			pOutputStr[pr++] = temp;
		}
		else{
			pOutputStr[pr++] = k + '0';
			pOutputStr[pr++] = temp;
			i += k - 1;
		}
	}

	pOutputStr[pr] = '\0';
}
int main()
{
	char strIn[100],strOut[100];
	while(cin>>strIn){
		int len = strlen(strIn);
		stringZip(strIn, len, strOut);
		cout<<strOut<<endl;
	}
	system("pause");
	return 0;
}

3. Title description (50 points): 
Input the addition and subtraction expressions of positive integers within 100 through the keyboard, please write a program to output the operation result string.
The format of the input string is: "operand 1 operator operand 2", with a space between "operand" and "operator".

补充说明:
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 <stdlib.h>
using namespace std;
const int MAXSIZE = 100;
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

int main()
{
	char StrIn[MAXSIZE];
	char StrOut[MAXSIZE] = {0};
	long len = strlen(StrIn);
	while(gets(StrIn))
	{
		arithmetic(StrIn, len, StrOut);
	    cout<<StrOut<<endl;
	}
	system("pause");
	return 0;
}
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	int cnt=0,a,b,result;
	long n = lInputLen;
	char leftVal[MAXSIZE],opt[MAXSIZE],rightVal[MAXSIZE],bufTemp[6];
	char WrongCh[1] = {'0'};
	
	for(int i=0; i<n; i++)  //检查空格的个数
	{
		if(pInputStr[i] == ' ')
			cnt++;
	}
	if(cnt != 2)    //如果空格数不为2
	{
		strcpy(pOutputStr, WrongCh);
		return;
	}

	sscanf(pInputStr, "%s %s %s", leftVal, opt, rightVal);

	if(strlen(opt)>1 || (opt[0]!='+'&&opt[0]!='-')) //检查操作符是否符合要求
	{
		strcpy(pOutputStr, WrongCh);
		return;
	}

	for(int i=0; i<strlen(leftVal); i++)  //检查操作数是否符合要求
	{
		if(leftVal[i]>'9' || leftVal[i]<'0')
		{
			strcpy(pOutputStr, WrongCh);
			cout<<pOutputStr<<endl;
			return;
		}
	}
	
	for(int i=0; i<strlen(rightVal); i++)
	{
		if(rightVal[i]>'9' || rightVal[i]<'0')
		{
			strcpy(pOutputStr, WrongCh);
			return;
		}
	}

	a = atoi(leftVal);
	b = atoi(rightVal);
	switch(opt[0])
	{
	   case '+': 
		   result = a + b;
		   itoa(result, bufTemp, 10);
		   strcpy(pOutputStr, bufTemp);
	       break;
	   case '-': 
		   result = a - b;
		   itoa(result, bufTemp, 10);
		   strcpy(pOutputStr, bufTemp);
		   break;
	   default: 
		   break;
	}

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324443417&siteId=291194637