上网搜索了一下去年华为的在线机试的题,看到了一个人总结的题,还给出了代码,非常好,我也试着写了一下,简单的test case 都通过了
http://blog.youkuaiyun.com/hackbuteer1/article/details/11132567
题目:
一、题目描述(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打印输出
当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响。
1 void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){ 2 //char *record = (char*)malloc(lInputLen*sizeof(char)); 3 int record[26] = {0}; 4 while(*pInputStr!='\0'){ 5 int index = *pInputStr-'a'; 6 if(record[index]==0){ 7 *pOutputStr = *pInputStr; 8 pOutputStr++; 9 record[index]++; 10 } 11 pInputStr++; 12 } 13 *pOutputStr = '\0'; 14 }
二、题目描述(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 = 1; char cur_ch = '0'; int flag = 0; while(*pInputStr!='\0'){ if(cur_ch == *pInputStr){ count++; }else{ if(count > 1){ *(pOutputStr++) = count + '0'; } if(flag++){//avoid add 0 into outStr *(pOutputStr++) = cur_ch; } cur_ch = *pInputStr; count = 1; } pInputStr++; } if(count > 1){//对最后字符的处理 *(pOutputStr++) = count + '0'; *(pOutputStr++) = cur_ch; }else{ *(pOutputStr++) = cur_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 reverse(char*q){//字符串反转 char *end = q; while(*end!='\0'){ end++; } end--; while(q<end){ char temp = *q; *q = *end; *end = temp; q++; end--; } } void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){ int num1=0,num2=0; int flag = 0; while(*pInputStr==' '){//忽略空格 pInputStr++; } while(*pInputStr!=' '){//直到遇到空格,取出第一个操作数 num1 = 10*num1+(*pInputStr - '0'); pInputStr++; } //printf("num1=%d ,num2=%d \n",num1,num2); while(*pInputStr==' '){//忽略空格 pInputStr++; } char op = *(pInputStr++);//取出操作数 if(op!='+' && op!='-'){ flag = 1; } while(*pInputStr < '0' || *pInputStr > '9'){//遍历直到第二个操作数之前,检验操作符是否合法 if(*pInputStr!=' '){ flag = 1; break; } pInputStr++; } //printf("flag=%d \n",flag); if(flag){//如果操作符异常,直接返回结果 *(pOutputStr++) = '0'; *pOutputStr = '\0'; return; } while(*pInputStr!=' ' && *pInputStr!='\0'){//取出第二个操作数 num2 = 10*num2 + (*pInputStr - '0'); pInputStr++; } //printf("num1=%d ,num2=%d ,op=%c\n",num1,num2,op); if(op=='+'){ num1+=num2; }else{ num1-=num2; } //printf("num1=%d ,num2=%d ,op=%c\n",num1,num2,op); char* p = pOutputStr; int isNegative = 0; if(num1<0){ num1 = -1*num1; isNegative = 1; } while(num1/10>0){ int temp = num1%10; *(pOutputStr++) = temp + '0'; num1/=10; } *(pOutputStr++) = num1 + '0'; if(isNegative) { *(pOutputStr++) = '-'; } *pOutputStr = '\0'; reverse(p); }