华为机试题--简易消除

Posted jh.ding

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了华为机试题--简易消除相关的知识,希望对你有一定的参考价值。

1 简易消除

要求:删除一组数中连续的3个及3个以上的数字,输出删除后的结果。

输入一组数字1 1 1 2 3 4 4 4 3 5 5 5 3 6 7
输出2 6 7
要求:连续3个及3个以上的数字删除,输出最后的数字,若全部删除了,输出NONE

C语言代码:

#include<stdio.h>

 

int judge_delete(int a[], int n);

 

void main()

{

         int num;

         int data[]={1,1,1,2,3,4,4,4,3,5,5,5,3,6,7};

         num = sizeof(data)/sizeof(data[0]);

         judge_delete(data,num);  //递归调用

 

}//main

 

int judge_delete(int a[], int n)

{

         int i,j,k,count,flag,tmp;

         flag=0;

 

         if (n==0)

         {

                   printf("NONE");

                   return 0;

         }

         else if (n>0 && n<3)

         {

                   for (i=0;i<n;i++)

                   {

                            printf("%d ",a[i]);  //打印输出

                   }

                   return 0;

         }

 

         else //n>=3

         {

                   for(i=0;i<n;i++)

                   {

                            count=0;

                            for(j=i;j<n;j++)

                            {

                                     if(a[i]==a[j])

                                     {

                                               count++; //统计有多少个连续相等的数字

                                     }

                                     else

                                     {

                                               break;  //如果突然间不等了,就退出

                                     }

                            }

                            if(count>=3)

                            {

                                     flag=1;

                                     break;

                            }

                            //else

                   }

                  

         }

 

 

         if (flag==1)  //存在三个及三个以上相同的数字,需要执行删除功能

         {

                   tmp=a[i];

                   for(j=i;j<n;j++)

                   {

                            if(tmp==a[j])

                            {

                                     for (k=j;k<n-1;k++)

                                     {

                                               a[k]=a[k+1];

                                              

                                     }

                                     j--;

                                     n--;

                            }

                            else

                            {

                                     break;

                            }

                   }

 

                   judge_delete(a,n);  //执行递归

         }

         else//flag==0

         {

                   for (j=0;j<n;j++)

                   {

                            printf("%d ",a[j]);

                   }

         }

 

         return 0;

 

}//judge

以上是关于华为机试题--简易消除的主要内容,如果未能解决你的问题,请参考以下文章

华为OD机试 - 简易压缩算法(C 语言解题)独家

独家提供华为OD机试 C 语言题解 - 简易内存池2

华为OD机试题(不间断更新)

华为OD机试题 - 分奖金(JavaScript)

华为OD机试题 - 新学校选址(JavaScript)

华为OD机试题 - 求最大数字(JavaScript)