1-华为招聘--软件工程师上机考试--字符串过滤,字符串压缩,整数运算

  1. 一、
  2. /*
  3. 题目描述(60分):
  4. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
  5. 比如字符串“abacacde”过滤结果为“abcde”。

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

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

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

  12. 示例 
  13. 输入:“deefd” 输出:“def”
  14. 输入:“afafafaf” 输出:“af”
  15. 输入:“pppppppp” 输出:“p”
  16. */

  17. /* main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出*/
  18. /* 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可 */
  19. /* 该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响*/
一、  stringFilter函数
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
  int i,j,flag,k=0;
  pOutputStr[0]=pInputStr[0];
  for(i=1;i<lInputLen;i++)
     {
         flag=1;
         for(j=0;j<=k;j++)
            {
              if(pOutputStr[j]==pInputStr[i])
                    flag=0;
            }
         if(flag==1)
         {
           k=k+1;
           pOutputStr[k]=pInputStr[i];
         }
     }
   pOutputStr[k+1]='\0';
}
方法二:
   void stringFilter(const char *pInputStr,long lInputLen, char *pOutputStr)
{
    const char* ptr = pInputStr;  
    char* output = pOutputStr;  
    int a[26] = {0};  
    while( *ptr != '\0')  
    {  
        a[*ptr - 'a']++;  
        if(a[*ptr - 'a'] > 1)  
        {  
            ptr++;  
        }  
        else  
        {  
            *output = *ptr;  
            ptr++;  
            output++;  
        }             
    }  
    *output = '\0';  
}


二、
  1. /*
  2. 题目描述(40分):
  3. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
  4. 压缩规则:
  5. 1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
  6. 2. 压缩字段的格式为"字符重复的次数 字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"

  7. 要求实现函数: 
  8. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);

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

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

  13. 示例 
  14. 输入:“cccddecc” 输出:“3c2de2c”
  15. 输入:“adef” 输出:“adef”
  16. 输入:“pppppppp” 输出:“8p”
  17. */
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
    const char* ptr=pInputStr;
    char *result=pOutputStr;
    int k=0;
    char c= *ptr;
    while(*ptr !='\0')
    {
        if(c == *ptr)
        {
            ptr++;
            k++;
        }
        else
        {
            if(k>1)
            {
                *result++=(char)(k+'0');
            }
            *result++=c;
            k=1;
            c=*ptr++;
        }
    }
    if(k>1)
    {
        *result++=(char)(k+'0');
    }
    *result++=c;
    *result='\0';
}

三、
  1. /*
  2. 题目描述(50分): 
  3. 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
  4. 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

  5. 补充说明:
  6. 1. 操作数为正整数,不需要考虑计算结果溢出的情况。
  7. 2. 若输入算式格式错误,输出结果为“0”。

  8. 要求实现函数: 
  9. void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

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

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

  14. 示例 
  15. 输入:“4 7” 输出:“11”
  16. 输入:“4 - 7” 输出:“-3”
  17. 输入:“9 7” 输出:“0” 注:格式错误
  18. */
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
    const char *input = pInputStr;
    char * output = pOutputStr;
    char* data1= (char*) malloc(lInputLen);
    char* left=data1;
    char* right;
    int d1,d2;
    char yuns;
    while (*input!='\0')
    {
        if((*input=='+')||(*input=='-'))
        {
            yuns=*input++;
            break;
        }
        else
        {
            *left++=*input++;
        }    
    }
    *left='\0';
    input++;
    if((*data1<='0')||(*data1>='9')||(( *input<='0')||(*input>='9')))
    {
        *output++='0';
        *output='\0';
        return;
    }
    d1=atoi(data1);
    d2=atoi(input);
    if(yuns=='+')
        itoa(d1+d2,output,10);
    else
        itoa(d1-d2,output,10);
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值