CCF CSP201612-1中间数(c++100)

CCF CSP201612-1中间数

CCF CSP201612-1中间数(c++100)

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int arr[maxn];
int main(){
	int n;
	cin>>n;
	bool flag = false;
	for(int i = 0; i < n; i++){
		int num;
		cin>>num;
		arr[i] = num;
	}
	sort(arr,arr+n);
	if(n%2==1){
		int count1 = 0, count2 = 0;
		for(int i = n/2-1; i >= 0; i--){
			if(arr[i] == arr[n/2]) count1++;
			else break;
		}
		for(int i = n/2+1; i < n; i++){
			if(arr[i] == arr[n/2]) count2++;
			else break;
		}
		if(count1 == count2) cout<<arr[n/2];
		else cout<<"-1";
		cout<<endl;
	}
	else{
		if(arr[n/2-1] != arr[n/2]) cout<<"-1"<<endl;
		else{
			int count1 = 0, count2 = 0;
			for(int i = n/2-2; i >= 0; i--){
				if(arr[i] == arr[n/2]) count1++;
				else break;
			}
			for(int i = n/2+1; i < n; i++){
				if(arr[i] == arr[n/2]) count2++;
				else break;
			}
			if(count1 == count2) cout<<arr[n/2];
			else cout<<"-1";
			cout<<endl;
		}
	}
	return 0;
}
上一篇:安装Burn-P3过程种的一些问题


下一篇:通过libcurl实现https访问服务器