1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 bool com(int a[],int n,int x) 6 { 7 int l = 0,r = 0; 8 for(int i = 0;i < n/2;i ++) 9 if(a[i] < x) l++; 10 for(int i = n-1;i > n/2;i --) 11 if(a[i] > x) r++; 12 if(l == r) 13 return true; 14 else 15 return false; 16 } 17 int main() 18 { 19 int n; 20 cin>>n; 21 int a[1010]; 22 for(int i = 0; i < n; i ++) 23 cin>>a[i]; 24 sort(a,a + n); 25 int res = a[n/2]; 26 if(com(a,n,res)) 27 cout<<res<<endl; 28 else 29 cout<<"-1"<<endl; 30 return 0; 31 }
原文:http://www.cnblogs.com/lca1826/p/6403205.html