华为2013校园招聘上机笔试题-杭州
今年的机试已经要第二轮了,搜索网上资源,备考。
2013华为校园招聘机试题9月10日题(杭州):
机试题目及解答来源:http://blog.chinaunix.net/uid-26868581-id-3334342.html
题目部分:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 3 题目描述(60分): 4 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 5 比如字符串“abacacde”过滤结果为“abcde”。 6 7 要求实现函数: 8 void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr); 9 10 【输入】 pInputStr: 输入字符串 11 lInputLen: 输入字符串长度 12 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 13 14 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 15 16 示例 17 输入:“deefd” 输出:“def” 18 输入:“afafafaf” 输出:“af” 19 输入:“pppppppp” 输出:“p” 20 */ 21 22 /* main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出*/ 23 /* 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可 */ 24 /* 该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响*/ 25 26 /* 27 题目描述(40分): 28 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。 29 压缩规则: 30 1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc". 31 2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz" 32 33 要求实现函数: 34 void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr); 35 36 【输入】 pInputStr: 输入字符串 37 lInputLen: 输入字符串长度 38 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 39 40 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 41 42 示例 43 输入:“cccddecc” 输出:“3c2de2c” 44 输入:“adef” 输出:“adef” 45 输入:“pppppppp” 输出:“8p” 46 */ 47 48 /* 49 题目描述(50分): 50 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 51 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 52 53 补充说明: 54 1. 操作数为正整数,不需要考虑计算结果溢出的情况。 55 2. 若输入算式格式错误,输出结果为“0”。 56 57 要求实现函数: 58 void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr); 59 60 【输入】 pInputStr: 输入字符串 61 lInputLen: 输入字符串长度 62 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 63 64 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 65 66 示例 67 输入:“4 + 7” 输出:“11” 68 输入:“4 - 7” 输出:“-3” 69 输入:“9 ++ 7” 输出:“0” 注:格式错误 70 */
解答:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define MAXCHAR 256 6 7 void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr) 8 { 9 int a[26] = {0}; 10 int pos = 0; 11 long i, j; 12 for(i = 0, j = 0; i < lInputLen; i++) 13 { 14 pos = pInputStr[i] - 'a'; 15 if(a[pos] == 0) 16 { 17 a[pos]++; 18 pOutputStr[j++] = pInputStr[i]; 19 } 20 } 21 } 22 23 void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr) 24 { 25 int i, j, k = 0, cnt = 0; 26 char buffer[30]; 27 28 for(i = 0; i < lInputLen;) 29 { 30 for(j = i + 1;;j++) 31 { 32 if(pInputStr[j] == pInputStr[i]) 33 cnt++; 34 else 35 break; 36 } 37 38 if(cnt != 0) 39 { 40 memset(buffer, 0, sizeof(buffer)); 41 itoa(cnt + 1, buffer, 10); 42 strcat(pOutputStr, buffer); 43 k += strlen(buffer); 44 i += cnt; 45 } 46 pOutputStr[k++] = pInputStr[i++]; 47 cnt = 0; 48 } 49 } 50 51 void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr) 52 { 53 int i, cnt = 0, a, b, result; 54 char ch[1] = {'0'}; 55 char op1[MAXCHAR], op[MAXCHAR], op2[MAXCHAR], buffer[4]; 56 for(i = 0; i < lInputLen; i++) 57 if(pInputStr[i] == ' ') 58 cnt++; 59 60 if(cnt != 2) //空格数不等于2 61 { 62 strcat(pOutputStr, ch); 63 return; 64 } 65 66 sscanf(pInputStr, "%s %s %s", op1, op, op2); 67 68 if(strlen(op) > 1 || (op[0] != '+' && op[0] != '-')) // 操作符有多个 69 { 70 strcat(pOutputStr, ch); 71 return; 72 } 73 74 for(i = 0; i < strlen(op1); i++) //操作数1是否有其他字符 75 { 76 if(op1[i] < '0' || op1[i] > '9') 77 { 78 strcat(pOutputStr, ch); 79 return; 80 } 81 } 82 83 84 for(i = 0; i < strlen(op2); i++) //操作数2是否有其他字符 85 { 86 if(op2[i] < '0' || op2[i] > '9') 87 { 88 strcat(pOutputStr, ch); 89 return; 90 } 91 } 92 93 a = atoi(op1); 94 b = atoi(op2); 95 96 switch(op[0]) 97 { 98 case '+': 99 result = a + b; 100 itoa(result, buffer, 10); 101 strcat(pOutputStr, buffer); 102 break; 103 case '-': 104 result = a - b; 105 itoa(result, buffer, 10); 106 strcat(pOutputStr, buffer); 107 break; 108 default: 109 break; 110 } 111 } 112 113 int main() 114 { 115 char pInputStr1[] = {"aaabbbcccdde"}; 116 char pInputStr2[] = {"aaabbcddde"}; 117 char pInputStr3[] = {"3 + 4"}; 118 char pOutputStr1[MAXCHAR] = {0}; 119 char pOutputStr2[MAXCHAR] = {0}; 120 char pOutputStr3[MAXCHAR] = {0}; 121 122 /* TODO: 调用被测函数 */ 123 stringFilter(pInputStr1, strlen(pInputStr1), pOutputStr1); 124 stringZip(pInputStr2, strlen(pInputStr2), pOutputStr2); 125 arithmetic(pInputStr3, strlen(pInputStr3), pOutputStr3); 126 127 /* TODO: 执行完成后可比较是否是你认为正确的值 */ 128 printf(pOutputStr1); //abcde 129 printf(pOutputStr2); //3a3b3c2de 130 printf(pOutputStr3); //7 131 return; 132 }
posted on 2012-09-18 15:58 Raphael Lou 阅读(767) 评论(0) 编辑 收藏 举报