Find Mode

Find Mode


Time Limit:

2000MS

 
Memory Limit:

65535K

Description

给出一个整数序列,求:其中重复出现次数最多的整数,以及其出现的次数。

Input

一个整数序列

Output

两个整数,中间有一个空格隔开。

第一个整数:重复出现次数最多的整数;

第二个整数:第一个整数的出现次数。

Sample Input

2 3 4 8 5 3 6 9 1 2 6 3 7 3

Sample Output

3 4

Hint

如果重复出现次数最多的整数有并列几个,那么请输出其中值最小的整数。

注意算法的时间效率和空间效率。

Source

 

代码:

1:

#include<stdio.h>
#include<algorithm>
using namespace std;
int n[5000000];
int main()
{
	int c=0,count= 1,ncount= 1,index = 0;

	while(scanf("%d",&n[c])!=EOF)
		c++;
	sort(n,n+c);
	
	for(int i=1;i<c;i++)
	{
		if (n[i] == n[i-1])
		{
			ncount++;
			if (ncount>count)
			{
				index = i;
				count = ncount;
			}
		}
		else
			ncount = 1;
		
	}
	printf("%d %d\n",n[index],count);


}


2:

#include<iostream>
using namespace std;

//分割数组
int partition (int A[],int l,int r,int& pivot)
{
	int temp;
	do{
		while(A[++l]<pivot);
		while((r!=0)&&(A[--r]>pivot));

		temp=A[l];
	    A[l]=A[r];
	    A[r]=temp;
	}while(l<r);

	temp=A[l];
	A[l]=A[r];
	A[r]=temp;
	return l;
}



//快速排序函数
void sort(int A[],int i,int j)
{ 
	int temp;

	if(j<=i)
		return;   //数组元素过少
	int pivotindex=(i+j)/2;   //查找轴值
	//将轴值放到最后
	temp=A[j];
	A[j]=A[pivotindex];
	A[pivotindex]=temp;

	int k=partition(A,i-1,j,A[j]);     //分割数组

	temp=A[j];
	A[j]=A[k];
	A[k]=temp;

	sort(A,i,k-1);
	sort(A,k+1,j);

}

int n[5000000];
int main()
{
	int c=0,count= 1,ncount= 1,index = 0;
	while(cin>>n[c++])
		if(cin.get()=='\n')
			break;

	sort(n,0,c);

	for(int i = 1 ; i < c ; i++){

		if (n[i] == n[i-1])
		{
			ncount++;
			if (ncount>count)
			{
				index = i;
				count = ncount;
			}
		}
		else
		{
			ncount = 1;
		}
	}
	cout<<n[index]<<" "<<count<<endl;


}

 

 

欢迎指教!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值