题目
BUPT 2014 计算机 ProblemA(oj)
给定一个长度为N的非降数列,求数列的中位数。
中位数:当有序数列的项数N为奇数时,处于中间位置的变量即为中位数;当N为偶数时,中位数则为处于中间位置的两个数的平均数。
用时:9min
题解
#include <bits/stdc++.h>
using namespace std;
#define Max 5000000
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int tmp=n;
vector<int>num;
while(tmp--)
{
int a;
cin>>a;
num.push_back(a);
}
sort(num.begin(),num.end());
if(n%2==1)
cout<<num[n/2]<<endl;
else
cout<<(num[n/2-1]+num[n/2])/2.0<<endl;
}
}
/*
2
4
1 1 2 2
5
1 1 2 2 3
*/