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

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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章