华为机试题目整理

来源:互联网 发布:平平无奇 古天乐 知乎 编辑:程序博客网 时间:2024/05/22 02:34

最近又到了招聘季了,现在也轮到我毕业了。针对华为的机试题目,我整理了一下自己写的一部分代码。</span>

代码参考别人和自己的理解写的,经过测试,可以运行。

如果有bug,或者更好的方法欢迎大家指出。

(一)

3. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。

比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数:

void stringFilter(const char *pInputStr,long lInputLen, char *pOutputStr);

【输入】 pInputStr:输入字符串

lInputLen: 输入字符串长度

【输出】 pOutputStr:输出字符串,空间已经开辟好,与输入字符串等长;

<pre name="code" class="objc">#include<stdio.h>#include<string.h>#include<malloc.h>void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);void main(){char *Instr, *Outstr;long int length = 0;Instr = (char *)malloc(sizeof(char));Outstr = (char *)malloc(sizeof(char));//printf("输入字符串的长度:");//scanf("%d", &length);printf("input the string:\n");scanf("%s", Instr);length = strlen(Instr);Outstr = (char *)malloc(length*sizeof(char));stringFilter(Instr, length, Outstr);printf("The output string is:%s\n", Outstr);}void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){int index_i;int index_o=0;int label[26] = { 0 };if (pInputStr == NULL || pOutputStr == NULL){return;}for (index_i = 0; index_i < lInputLen; index_i++){if (label[pInputStr[index_i] - 'a'] == 0){label[pInputStr[index_i] - 'a']=1;pOutputStr[index_o] = pInputStr[index_i];index_o++;}}pOutputStr[index_o] = '\0';}

4.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。

压缩规则:

1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".

2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"

要求实现函数:

void stringZip(const char*pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr: 输入字符串

lInputLen: 输入字符串长度

【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

#include<stdio.h>#include<string.h>void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);void main(){char *Instr, *Outstr;int length;Instr = (char *)malloc(sizeof(char));Outstr = (char *)malloc(sizeof(char));printf("Input the String:\n");scanf("%s", Instr);length = strlen(Instr);stringZip(Instr, length, Outstr);printf("The Output String is:\n%s",Outstr);}void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){int index_i = 0;int index_o = 0;int f = 1;if (pInputStr == NULL || pOutputStr == NULL){return;}for (index_i = 0; index_i < lInputLen;){if (pInputStr[index_i] != pInputStr[index_i + 1]){pOutputStr[index_o++] = pInputStr[index_i++];}else{while (pInputStr[index_i] == pInputStr[index_i + f]){f++;}pOutputStr[index_o++] = f + '0';pOutputStr[index_o++] = pInputStr[index_i];index_i = index_i + f;}f = 1;}pOutputStr[index_o] = '\0';}


5.通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。

输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:

1. 操作数为正整数,不需要考虑计算结果溢出的情况。

2. 若输入算式格式错误,输出结果为“0”。

要求实现函数:

void arithmetic(const char*pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr: 输入字符串

lInputLen: 输入字符串长度

【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

#include<stdio.h>#include<string.h>void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);void main(){char *Instr, *Outstr;int length;Instr = (char *)malloc(sizeof(char));Outstr = (char *)malloc(sizeof(char));printf("Input the num:\n");gets(Instr);length = strlen(Instr);arithmetic(Instr, length, Outstr);printf("The result is :\n%s", Outstr);}void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){int num1, num2;int out;int index_o = 0;char c;int cnt=0;if (pInputStr == NULL || pOutputStr == NULL){return;}for (int i = 0; i<lInputLen; ++i){if (pInputStr[i] == ' ')cnt++;}if (cnt != 2){pOutputStr[0] = '0';pOutputStr[1] = '\0';}if (sscanf(pInputStr, "%d %c %d", &num1, &c, &num2) != 3){pOutputStr[0] = '0';pOutputStr[1] = '\0';}if (c == '+'){out = num1 + num2;}else if (c == '-'){out = num1 - num2;}else{pOutputStr[0] = '0';pOutputStr[1] = '\0';}itoa(out, pOutputStr, 10);}


0 0
原创粉丝点击