(0903)2013华为校园招聘机试题

【题目】【代码实现】

1、
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数: 
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

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

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”
*/
[cpp]  view plain copy
  1. #include <stdlib.h>  
  2. #include <string.h>  
  3. //过滤函数  
  4. void stringFilter(const char *pInputStr, char *pOutputStr)  
  5. {  
  6.   
  7.     int a[26] = {0};  
  8.     const char* pstr = pInputStr;  
  9.     char* pResult = pOutputStr;  
  10.     while( *pstr != '\0')  
  11.     {  
  12.         a[*pstr - 'a']++;  
  13.         if(a[*pstr - 'a'] > 1)  
  14.         {  
  15.             pstr++;  
  16.         }  
  17.         else  
  18.         {  
  19.             *pResult = *pstr;  
  20.             pstr++;  
  21.             pResult++;  
  22.         }             
  23.   
  24.     }  
  25.     *pResult = '\0';  
  26.   
  27. }  

2、
/*
题目描述(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”

[cpp]  view plain copy
  1. //压缩字符串  
  2. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)  
  3. {  
  4.   
  5.     const char* pFirst = pInputStr;  
  6.     const char* pSecond = pInputStr+1;  
  7.     char* pResult = pOutputStr;  
  8.     int count = 0;  
  9.     while(*pSecond != '\0')  
  10.     {  
  11.           
  12.         if(*pSecond == *pFirst)  
  13.         {  
  14.             count++;  
  15.         }             
  16.         else  
  17.         {  
  18.             if(count > 0)  
  19.             {  
  20.                 *pResult++ = count+1 + '0';  
  21.             }                 
  22.             *pResult++ = *pFirst;  
  23.             count = 0;  
  24.         }  
  25.         pFirst++;  
  26.         pSecond++;  
  27.     }  
  28.     if(count > 0)  
  29.     {  
  30.         *pResult++ = count+1 + '0';  
  31.     }         
  32.     *pResult++ = *pFirst;  
  33.     *pResult = '\0';  
  34.   
  35. }  

3、
通过键盘输入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” 注:格式错误
[cpp]  view plain copy
  1. void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)  
  2. {  
  3.     char* pTemp = (char*)malloc(lInputLen);   
  4.     char* pLeft = pTemp;  
  5.     const char* pRight = pInputStr;  
  6.     while(*pInputStr != '\0')  
  7.     {  
  8.         if(*pInputStr == '+' || *pInputStr == '-')  
  9.         {  
  10.             pRight = pInputStr + 1;  
  11.             break;  
  12.         }  
  13.         else  
  14.         {  
  15.             *pTemp++ = *pInputStr++;              
  16.         }  
  17.     }  
  18.     *pTemp = '\0';  
  19.     if (pRight == pLeft || *pRight == '+' || *pRight == '-')  
  20.     {  
  21.         *pOutputStr++='0';  
  22.         *pOutputStr = '\0';  
  23.         return;  
  24.     }  
  25.     int L = atoi(pLeft);  
  26.     int R = atoi(pRight);  
  27.     int result;  
  28.     switch (*pInputStr)  
  29.     {  
  30.     case '+':  
  31.         result = L + R;  
  32.         break;  
  33.     case '-':  
  34.         result = L - R;  
  35.         break;  
  36.     default:  
  37.         result = 0;  
  38.         break;  
  39.     }  
  40.     itoa(result, pOutputStr, 10);         
  41.   
  42. }  

【测试】

[cpp]  view plain copy
  1. int main()  
  2. {  
  3.     char a[] = "youyouare";  
  4.     char a1[] = "deefd";  
  5.     char a2[] = "afafafaf";  
  6.     char a3[] = "ppppp";  
  7.     char* b = (char*)malloc(100);  
  8.     char* b1 = (char*)malloc(100);  
  9.     char* b2 = (char*)malloc(100);  
  10.     char* b3 = (char*)malloc(100);  
  11.       
  12.     stringFilter(a,b);  
  13.     stringFilter(a1,b1);  
  14.     stringFilter(a2,b2);  
  15.     stringFilter(a3,b3);  
  16.   
  17.     char t1[] = "cccddecc";  
  18.     char t2[] = "deefd";  
  19.     char t3[] = "pppppp";  
  20.     char rt1[15], rt2[15], rt3[15];  
  21.     stringZip(t1,0,rt1);  
  22.     stringZip(t2,0,rt2);  
  23.     stringZip(t3,0,rt3);  
  24.   
  25.     char s1[] = "4+7";  
  26.     char s2[] = "4-7";  
  27.     char s3[] = "9++7";  
  28.     char st1[15], st2[15], st3[15];   
  29.     arithmetic(s1, strlen(s1), st1);  
  30.     arithmetic(s2, strlen(s2), st2);  
  31.     arithmetic(s3, strlen(s3), st3);  
  32.   
  33.   
  34. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值