好未来:n个数里出现次数大于等于n/2的数

题目链接: n个数里出现次数大于等于n/2的数

直接排个序,找中间的数就好了,排完序考虑最极端的情况,如果出现次数大于等于n/2的数如果在开头或者结尾,它也会出现在最中间,更别说其它情况了,然后用容斥原理也可以做。

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e6+5;
int num[maxn];
int main()
{
	int n,i=0;
	while(cin>>n)
	{	
		num[i]=n;
		i++;
	}
	sort(num,num+i+1);
	cout<<num[i/2]<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/q1122333/article/details/82961828