zoukankan      html  css  js  c++  java
  • 【乱搞】【AOJ-149】简易版最长序列

    Description
    给你一组数(未排序),请你写设计一个程序:求出里面个数最多的数。并输出这个数的长度。
    例如:给你的数是:1、 2、 3、 3、 4、 4、 5、 5、 5 、6, 其中只有6组数:1, 2, 3-3, 4-4, 5-5-5 and 6.
    最长的是5那组,长度为3。所以输出3。

    Input
    第一行为整数t((1 ≤ t ≤ 10)),表示有n组测试数据。
    每组测试数据包括两行,第一行位数组的长度n (1 ≤ n ≤ 10000)。第二行为n个整数,所有整数Mi的范围都是(1 ≤ Mi ≤ 2^32)

    Output
    对应每组数据,输出个数最多的数的长度。

    Sample Input
    1
    10
    1 2 3 3 4 4 5 5 5 6
    

     
    Sample Output
    3
    思路:
    1、先对数组进行排序
    2、排序后对数组中不同数字的个数进行统计
    3、求出数字个数的最大值
     
    参考代码:
    #include<stdio.h>
    #include<stdlib.h>
    #define LEN 10000+10
    void sort(int *a,int n);//冒泡排序
    
    int main()
    {
        int a[LEN],b[LEN]={0};
        int t,n,k=0,i,j,temp;
        scanf("%d",&t);
        
        while(t--)
        {
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
                b[i]=1;
            }
            
            sort(a,n);//对数组进行排序
            
            for(k=0,j=1;j<n;j++)//统计字数个数
            {
                if(a[j]==a[j-1]) 
                    b[k]++;
                else 
                    k++;
            }
    
            temp=b[0];
    
            for(i=0;b[i];i++)//找出字数最大值
            {
                if(temp<b[i])
                    temp=b[i];
            }
            printf("%d
    ",temp);
    
            k=0;
        }
        //system("PAUSE");
        return 0;
    }                  
    
    void sort(int *a,int n)
    {
        int i,j;
        int temp;
        for(i=0;i<n-1;i++)
        {
            for(j=0;j<n-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }
     
  • 相关阅读:
    mac与ip为什么同时存在
    tcp四次挥手
    tcp三次握手
    GET与POST的区别
    Servlet.service() for servlet [jsp] in context ....错误
    c3p0连接数据库时注意事项
    finalize()及垃圾回收
    composer 安装新包失败的原因之一
    如何使用优酷开放平台获取视频播放列表
    php解析优酷网上的视频资源去广告
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3467063.html
Copyright © 2011-2022 走看看