您的位置:首页 > 其它

2014届华为校招软件类机试题(机试时间:2013年9月)

2014-08-17 17:04 204 查看
题目来源:http://blog.csdn.net/hackbuteer1/article/details/11132567

3道题目如下:

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

 

原链接博主给出的解中使用了 itoa(),atoi(),strlen()等库函数(指在功能函数实现中使用,main函数中只作为测试用不关心),这就需要包含相关的头文件,可能不太符合题目要求仅仅实现功能函数要求。

这里给出个人对三道题的解法:

题目1
stringFilter 解:

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){
bool flag[256];
int index = 0;
for(int i=0;i<sizeof(flag);i++)
flag[i] = false;
for(int i=0;i<lInputLen;i++){
char tmp = pInputStr[i];
if(tmp<'a'||tmp>'z') //错误处理
break;
if(flag[tmp]==false){
pOutputStr[index++] = tmp;
flag[tmp]  = true;
}
}
pOutputStr[index]  = '\0';

}

题目2 stringZip 解:

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){
long cnt=0;
int index_in = 0,index_out = 0;
//sprintf(NULL);
char curChar=0;

curChar = pInputStr[0];
cnt = 1;
index_in = 1;
for(int i=1;i<=lInputLen;i++){
if((i!=lInputLen)&&(pInputStr[index_in]==curChar))
cnt++;
else{
//int divid= (1e9);  //最大的32位int整数 为2147483647 ,约为 2e9
if(cnt==1){

}
else{
char stack[100]; //栈
int  top=0;
while(cnt){
stack[top++] = cnt%10+ '0';
cnt /=10;
}
while(top){
pOutputStr[index_out++] = stack[--top];
};
}
pOutputStr[index_out++] = curChar;
curChar = pInputStr[index_in];
cnt = 1;

}
index_in++;

}
pOutputStr[index_out] = '\0';

}


题目3 arithmetic 解:

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){
int operand1=0,operand2=0;
char addOrMinus;
int index_in=0,index_out=0;
char charTmp;
int bits=0;  //位数,提取 操作数1 和操作数2 时作为辅助用;
//maxCnt = 3;

if(pInputStr==NULL || lInputLen==0 || pOutputStr==NULL)
return;
bits= 0;
while(++bits<=3){
charTmp=pInputStr[index_in];
if(charTmp ==' '){
if(bits==1)  //起始字符为空格,
goto wrong;
else
break;      //
}

if(charTmp<'0'||charTmp>'9')
goto wrong;
operand1 *=10;
operand1 += (charTmp-'0');
index_in++;
}
if(operand1>100)
goto wrong;
if(pInputStr[index_in++]!=' ')
goto wrong;
charTmp = pInputStr[index_in++];
if(charTmp =='+' || charTmp=='-')
addOrMinus = charTmp;
if(pInputStr[index_in++]!=' ')
goto wrong;
bits= 0;
while(++bits<=3){
charTmp=pInputStr[index_in];
if(charTmp =='\0'){
if(bits==1)
goto wrong;
else
break;      //
}
if(charTmp<'0'||charTmp>'9')
goto wrong;
operand2 *=10;
operand2 += (charTmp-'0');
index_in++;
}
if(operand2>100)
goto wrong;

int result = addOrMinus=='+'? operand1+operand2: operand1-operand2;

if(result<0){
pOutputStr[index_out++]='-';
result *= -1;
}
int divid=100;
if(result !=0){  //并非真正出错,处理方便而已
//result 不为0,
char stack[10];
int  top = 0;
while(result){
stack[top++] = result % 10 + '0';
result /=10;
}
while(top){
pOutputStr[index_out++] = stack[--top];
}
pOutputStr[index_out]= '\0';
return;
}
//result == 0 的情况处理如下
wrong:
pOutputStr[0]='0';
pOutputStr[1]='\0';

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  华为 软件