1153: 简易版最长序列

1153: 简易版最长序列

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1820   Solved: 613

Submit Status Web Board

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

1101 2 3 3 4 4 5 5 5 6

Sample Output

3

HINT

Source        



#include<iostream>
#include<cstdlib>  //预定义一些常用函数
using namespace std;

int nCase;
int  n;
unsigned array[100001];

int compare (const void *a , const void *b)
{
    int *pa=(int *)a;
	int *pb=(int *)b;
	return (*pa)-(*pb);  //从小到大排序
}

 int main ()
 {
     while( cin>>nCase ) //输入nCase组数
	 {
	         while ( nCase-- )
			 {
			       cin>>n; //输入n个数
                   for (int i=0 ; i<n ; i++)
					   cin>>array[i];
				   //快速排序的标准库函数qsort,在stdlib中声明
				   //array待排序数组的起始位置,n待排序数组元素的个数,
				   //第三个是指待排序数组每个元素的大小,最后一个参数指向一个比较函数
				   qsort(array,n,sizeof(unsigned),compare);

				   int len =0;
				   int index1 = 0 ;
				   int index2 = 1 ;

				   while ( index2 <= n )
				   {
				          if ( array[index1] == array[index2] )
						  {
						        index2++;
						  }
						  else
                          {
						       if ( len < index2 - index1 )
								   len = index2 - index1;
							       index1=index2;
								   index2++;
						  }
				   }

				   if ( len == 0 )
					   cout<<n<<endl;
				   else
					   cout<<len<<endl;
			 }	 
	 }
 return 0;
 }



#include<iostream>
using namespace std;
unsigned  a[100001],b[100001];
void fun();
int main ()
{
      int  T;
	  cin>>T;
	  while ( T-- )
	  {
	       fun();
	  }
return 0;
}

void fun()
{
        int  i,j,n,max=0;
        cin>>n;
		for (i=0;i<n;i++)
		{
		      cin>>a[i]; //存数的数组a[i],计算次数的数组吧
			  b[i]++;//a[i]在第i位时出现的次数
			  for (j=0;j<i;j++)   //内层循环检索前i-1个与i相同的的次数,用b[j]来记录次数
			  {                   //并将前i-1的数出现的次数b[i]=0归零
			       if (a[i]==a[j])
				   {
					   b[j]++;//a[j]此时在j位时出现的次数
			           b[i]=0;
				   }
			  }
		}
		for ( i=0 ; i<n ; i++)
			if ( b[i] > max )  //找出最大次数的i坐标
				max=b[i];

		for ( i=0 ; i<n ; i++)
			if ( b[i]==max )     //找到数后输出这个数a[i],和a[i]出现的次数b[i]
			//	cout<<a[i]<<' '<<b[i]<<endl;
            cout<<b[i]<<endl;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值