Description
给出一长度为n的序列,输出其中位数
Input
第一行为一整数n表示序列长度,之后n行每行一整数表示该序列(1<=n<=250000)
Output
输出该序列的中位数
Sample Input
4
3
6
4
5
Sample Output
4.5
Solution
快排一下即可
Code
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 255555
int n,a[maxn];
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+n+1);
double ans;
if(n&1)ans=a[(n+1)/2];
else ans=a[n/2]/2.0+a[n/2+1]/2.0;
printf("%.1lf\n",ans);
}
return 0;
}