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
#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<stdlib.h>
//冒泡排序
void paixu(int a[],int n){    
	int i,j,t;
	int flag=1;             //用flag来标志
	for(i=1;i<n;i++){
		for(j=0;j<n-i;j++){
			if(a[j]>a[j+1]){
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
				flag=0;        //一趟下来均没有经过交换操作,说明序列已有序
			}
		}
		if(flag)break;
	}
}
int main(){
	int t,n,i,flag,cnt,max;
	int a[10003]; 
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		memset(a,0,sizeof(int)*n);
		flag=0;
		cnt=1;
		max=INT_MIN;
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);				
		}
		paixu(a,n);
		for(i=0;i<n;i++){
			if(flag==a[i]){
				cnt++;             //与上一个数比较,相同则计数加1,否则置为1
			}
			else
				cnt=1;
			flag=a[i];
			max=max>cnt?max:cnt;
		}
		printf("%d\n",max);
	}
	return 0;
}
发布了72 篇原创文章 · 获赞 3 · 访问量 1847

猜你喜欢

转载自blog.csdn.net/vivi_cin/article/details/104649284
今日推荐