试题编号: 201612-1
试题名称: 中间数
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
在一个整数序列a1, a2, …, an中,如果存在某个数,大于它的整数数量等于小于它的整数数量,则称其为中间数。在一个序列中,可能存在多个下标不相同的中间数,这些中间数的值是相同的。
给定一个整数序列,请找出这个整数序列的中间数的值。
输入格式
输入的第一行包含了一个整数n,表示整数序列中数的个数。
第二行包含n个正整数,依次表示a1, a2, …, an。
输出格式
如果约定序列的中间数存在,则输出中间数的值,否则输出-1表示不存在中间数。
样例输入
6
2 6 5 6 3 5
样例输出
5
样例说明
比5小的数有2个,比5大的数也有2个。
样例输入
4
3 4 6 7
样例输出
-1
样例说明
在序列中的4个数都不满足中间数的定义。
样例输入
5
3 4 6 6 7
样例输出
-1
样例说明
在序列中的5个数都不满足中间数的定义。
评测用例规模与约定
对于所有评测用例,1 ≤ n ≤ 1000,1 ≤ ai ≤ 1000。
#include <iostream>
#include <vector>
#include <algorithm>
#define _for(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int main()
{
int n, x;
int leftN = 0;
int rightN = 0;
cin >> n;
vector<int> v;
_for(i, 0, n)
{
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int mid = v[n / 2];
_for(i, 0, n)
{
if (v[i] < mid)
leftN++;
if (v[i] > mid)
rightN++;
}
if (leftN == rightN)
cout << mid;
else
cout << -1;
//system("pause");
return 0;
}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNyZuBnL3gTMxITMyETMyATOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)