AOJ149 简易版最长序列

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\n",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;
            }
        }
    }
}

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值