ZZULIOJ:1153: 简易版最长序列

题目描述

给你一组数(未排序),请你设计一个程序:求出里面个数最多的数。并输出这个数的长度。
例如:给你的数是: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。

 

输入

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


 

输出

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

样例输入 Copy
1
10
1 2 3 3 4 4 5 5 5 6
样例输出 Copy
3

源代码

//本题为数组模拟题 
//对于本题有个疑惑之处,因为本人写题习惯用c++,所以都是cin和cout来输入输出
//这道题使用cin错了十几次,仔细重复检查了方法确实没错
//最后一句一句试出来在对于整型数组的输入(有!!!的那一句)时使用了cin错误
//但是使用scanf却正确,希望有大佬可以解答一下 
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int a[N];
int main()
{
    int t;
    cin >> t;
    while(t -- )
    {
    	//因多实例测试,重置数组a中的元素 
        memset(a,0,sizeof(a));
        int len = 1;
        int ans = 1;
        int n;
        cin >> n;
        for(int i = 0;i < n ;i ++ )scanf("%d",&a[i]);//!!!!!!!!!!
        sort(a,a + n);//排序 
        for(int i = 1;i < n;i ++ )
        {
            if(a[i] == a[i - 1])len ++ ;//若相同则长度加1
            else len = 1;//若不同则置位1 
            ans = max(ans,len);//答案不断取取最大值即可 
        }
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/126077296