"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
3 5 1
题意:输入n(n为奇数)再输入n个数,输出在n个数中出现次数不少于(n+1)/2次,本来想着用个结构体和map试一试,但是想到输入数为0时会可能会出现异常。博客后,发现一个神奇的算法,直接快排,输出第(n+1)/2个数,好佩服。
#include<iostream> #include<algorithm> using namespace std; int a[1000000]; int main() { int n,i; while(cin>>n) { for(i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n); cout<<a[(n+1)/2]<<endl; }}