zoukankan      html  css  js  c++  java
  • 编程题--简易消除

    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

  • 相关阅读:
    Spring Boot重定向的使用方法
    Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
    Jmeter性能测试之分布式(五)
    Jmeter性能测试之Monitor监控(四)
    Jmeter性能测试之关联(三)
    Jmeter性能测试之参数化(二)
    JVM 内存溢出(转载~)
    MySql workbeach 更改侧边栏大小
    Mybati example generatorConfig.xml 配置详解
    Rvm 进行gem安装时必须输入密码Your user account isn't allowed to install to the system RubyGems 解决方案
  • 原文地址:https://www.cnblogs.com/jhding/p/5846667.html
Copyright © 2011-2022 走看看