logo资料库

2014年华为校园招聘笔试算法试题汇总.doc

第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
资料共10页,剩余部分请下载后查看
2014 年华为校园招聘笔试算法试题汇总 1.通过键盘输入一串小写字母(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 打印输出 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改, 但是不要改变函数原型。 一定要保证编译运行不受影响 [cpp] view plaincopy ////////////////////////////////////////////////////////////////////////// #include #include using namespace std; bool g_flag[26]; void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr) { assert(pInputStr != NULL);
int i = 0; if (pInputStr == NULL || lInputLen <= 1) { } return; const char *p = pInputStr; while(*p != '\0') { } if (g_flag[(*p - 'a')]) { }else{ p++; pOutputStr[i++] = *p; g_flag[*p - 'a'] = 1; p++; } pOutputStr[i] = '\0'; } int main() { memset(g_flag,0,sizeof(g_flag)); char input[] char *output = = "abacacde"; new char[strlen(input) + 1]; stringFilter(input,strlen(input),output); cout<
出席的重复字母进行压缩,并输出压缩后的字符串。 压缩规则: 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” [cpp] view plaincopy ////////////////////////////////////////////////////////////////////////// #include #include using namespace std; void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr) = pInputStr; { const char int num = *p 1; int i = 0; p++; while(*p != NULL) {
while(*p == *(p-1)&& *p != NULL) { } num++; p++; if (num > 1) { int size int temp = = 0; num; while(num) { } size++; num /= 10; num = 1; //计算位数 for (int j = size; j > 0; j--) { } i pOutputStr[i+j-1] = '0'+ temp%10; temp /= 10; +=size; pOutputStr[i++] = *(p-1); p++; }else{ pOutputStr[i++] = *(p-1); p++; } } pOutputStr[i] = '\0'; }
int main() { } char input[] char *output = = "cccddecc"; new char[strlen(input) + 1]; stringZip(input,strlen(input),output); cout< using namespace std; void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{ const char *input = pInputStr; char *output = pOutputStr; int sum = 0; int operator1 int operator2 = = 0; 0; char *temp = new char[5]; char *ope = temp; while(*input != ' ') //获得操作数 1 { } sum = sum*10 + (*input++ - '0'); input++; operator1 = sum; sum = 0; while(*input != ' ') { } *temp++ = *input++; input++; *temp = '\0'; if (strlen(ope) > 1 ) { } *output++ = '0'; *output = '\0'; return;
while(*input != '\0') //获得操作数 2 { } sum = sum*10 + (*input++ - '0'); operator2 = sum; sum = 0; switch (*ope) { case '+':itoa(operator1+operator2,pOutputStr,10); break; case '-':itoa(operator1-operator2,pOutputStr,10); break; default: *output++ = '0'; *output = '\0'; return; } } int main() { } char input[] = "4 - 7"; char output[] = " "; arithmetic(input,strlen(input),output); cout<
[cpp] view plaincopy //华为 2014 年机试题 1:输入 1--50 个数字,求出最小数和最大数的和 //输入以逗号隔开 #include #define N 50 void sort(int a[],int n); int main(void) { char str[100]; int a[N]={0}; gets(str); //要点 1:动态的输入 1--50 个整数,不能确定个数,只能 用字符串输入,然后分离出来 int i=0; int j=0; int sign=1; while(str[i]!='\0') { if(str[i]!=',') //输入时要在半角输入 { if(str[i] == '-') //要点:2:有负整数的输入 { } { // i++; //易错点 1 sign=-1; else if(str[i]!='\0') // 不 用 else 的 话 , 负 号 也 会 减 去 a[j]=a[j]*10 + str[i]-'0'; //要点 3:输入的可以是 ‘0’ 多位数
分享到:
收藏