您的位置:首页 > Web前端

华为2013校园招聘上机试题(杭州)

2012-09-19 20:23 369 查看
1)杭州2012-9-1上机3个试题

/* 请在这里实现下列函数, c c++语法不限, 最后需要保证程序编译连接通过, 并生成test.exe文件. */

/* 相关宏定义及函数声明见'func.h'头文件 */

//#include "stdafx.h"

#include <stdio.h>

#include <memory.h>

#include <string.h>

#include <stdlib.h>

//#include "func.h"

#define MAXCHAR 100

/* 请按照要求实现下列函数,pOutputStr的长度足够大*/

#include "string.h"

#include "stdlib.h"

/*

题目描述(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打印输出*/

/* 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可 */

/* 该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响*/

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
char a[26]={0};

while(*pInputStr)
{
a[*pInputStr-'a']++;
if(a[*pInputStr-'a']>1)
{
pInputStr++;
}
else
{
*pOutputStr = *pInputStr;

pInputStr++;
pOutputStr++;
}
}

*pOutputStr = '\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”

*/
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int count=0;
char ch=*pInputStr;

while(*pInputStr)
{
if(*pInputStr!=ch)
{
if(count>0)
*pOutputStr++ = count+1+'0';

*pOutputStr++ = ch;

count = 0;
ch = *pInputStr;
}
else
{
count++;
}

pInputStr++;
}

if(count>0)
*pOutputStr++ = count+'0';

*pOutputStr++ = ch;

*pOutputStr='\0';
}


/*

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

*/
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int left=0,right=0,result;
char op,buffer[4];

cout << sscanf(pInputStr,"%d %c %d",&left,&op,&right);

if(left>0 && right>0 && (op=='+'||op=='-'))
{
if(op=='+')
result= left + right;
if(op=='-')
result= left - right;

itoa(result,pOutputStr,10);
//strcat(pOutputStr,buffer);
}
}

int main()
{
//char str[]="  chen qun x xc xxxx";
//char *out;
//changeStr(str,str);
//cout << str << endl;;

//char Input[]={"cccbbbaaadf"};
//stringFilter(Input,strlen(Input), Input);
//cout << Input << endl;

//char Input[] ={"cccbbbaaadf"};
//stringZip(Input,strlen(Input), Input);
//cout << Input << endl;

//char Input[] ="11 - 5";
//arithmetic(Input,strlen(Input), Input);
//cout << Input << endl;

return 0;
}


1)杭州华为2012-9-19上机3个试题

1)判断一个整数是否为素数。

2)求一个整数数组中,大于它们平均数的元素的个数。

3)判断一个整数是否为回文数字。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息