首页 > 代码库 > 2016年华为机试题--简易消除

2016年华为机试题--简易消除

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

2016年华为机试题--简易消除