华为历年机试题型总结系列(三)

6. 统计出现最大次数的数字,输出该数字以及该数字出现的次数

输入:323324423343        输出:3,6

#include<stdio.h>
#include<string.h>

int main(void)
{
    char pInputStr[20];
    int i,j,StrLength,max_times=0,max_number=0;
    int pInputInt[20];
    int pFrequents[10]={0};
    
     printf("Input the number sequences:\n");
     gets(pInputStr);
     StrLength=strlen(pInputStr);

     for(i=0;i<StrLength;++i)
     {
             pInputInt[i]=pInputStr[i]-48;   //将对应的数字字符串转换成数字
             for(j=0;j<=9;++j)
             {
                   if(pInputInt[i]==j)
                   ++pFrequents[j];         //统计数字j对应出现的次数,并0-9顺序依次存入数组中
             }
      }

      for(k=0;k<=9;++k)
     {
           max_times=(pFrequents[k]>max_times)?pFrequents[k]:max_times;     //遍历输出数组中最大值,最大值为出现次数最大值
     }

      for(k=0;k<=9;++k)
     {
            if(max_times==pFrequents[k])
                max_number=k;          //返回最大次数对应的数字
     }

     printf("the max times number: %d, and the times: %d",max_number,max_times);

     return 0;
}


7.字符串过滤

输入:abbadce       输出:abdce

#include<stdio.h>
#include<string.h>

void StrFilter(char *pInputStr, int StrLength, char *pOutputStr)
{
     int i,j=0;
     bool hash[26]={0};

     for(i=0;i<StrLength;)
     {
          if(hash[pInputStr[i]-'a']==false)         //如果后面出现不同的字符
          {
                 hash[pInputStr[i]-'a']=true;
                 pOutputStr[j++]=pInputStr[i++];
          }else
                ++i;
     }

     pOutputStr[j]='\0';
}

int main(void)
{
     char pInputStr[20],pOutputStr[20];
     int StrLength;
     
     printf("Input the string:\n");
     gets(pInputStr);
     StrLength=strlen(pInputStr);
     
     StrFilter(pInputStr,StrLength,pOutputStr);
     puts(pOutputStr);
    
     return 0;
}

9. 将整数倒序输出,剔除重复数据

输入:12336544  输出:456321  如果最后是0,则不输出,输入:1750 输出:571 如果是负数,比如输入:-175 输出:-571

#include<stdio.h>
#include<string.h>

void NumberReverse(char *pInputStr, int StrLength, char *pOutputStr)
{
      int i,j=0,count;
      bool hash[10]={0};

      for(i=StrLength-1;i>=0;)
      {
            if(pInputStr[StrLength-1]=='0')  //如果最后是0
             {
                   --i;
                   --StrLength;         //不减1的话,重复执行此句会出错
              }

              if(hash[pInputStr[i]-'1']==false)   //如果前面没有重复的数字
              {
                     hash[pInputStr[i]-'1']=true;
                     pOutputStr[j++]=pInputStr[i--];
               }else
                     --i;
      }

       if(pInputStr[0]=='-')  //如果输入负号
       {
              for(count=j;count>=1;--count)
              {
                   pOutputStr[count]=pOutputStr[count-1];           //向后移位,空出第0位
              }
                   pOutputStr[0]='-';  
                   ++j;           //之前判断负号时,没++j
        }

        pOutputStr[j]='\0';
}

int main(void)
{
     char pInputStr[20],pOutputStr[20];
     int StrLength;
     printf("Input the number sequences:\n");
     
     gets(pInputStr);
     StrLength=strlen(pInputStr);
     NumberReverse(pInputStr,StrLength,pOutputStr);
     puts(pOutputStr);

     return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值