码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-06 18:19:51      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

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年华为机试题--简易消除

标签:

原文地址:http://www.cnblogs.com/jhding/p/5846667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!