这题,就是给了一些数,求中位数。说起来我就想哭,我的快速排序是没写对吗还是怎么的,为什么我用快排就会超时T.T,然后我用了STL的algorithm的sort,就一行代码就AC了,我的心里真是十万个草泥马,我明明是照着课本敲的快排啊,这到底是为什么T.T
#include<iostream>
#include<algorithm>
#define N 300000
using namespace std;
int num[N];
int Part( int low, int high) {
int p=num[low];
while(low<high) {
while(low<high && num[high]>p) high--;
num[low]=num[high];
while(low<high && num[low]<p) low++;
num[high]=num[low];
}
num[low]=p;
return low;
}
void QSort( int start, int end) {
if(start<end) {
int loc=Part(start,end);
QSort(start,loc);
QSort(loc+1,end);
}
}
int main() {
int n;
double sum;
cin>>n;
for( int i=0; i<n; i++)
cin>>num[i];
//QSort(0,n-1);
sort(num,num+n);
/* for(int i=0; i<n; i++)
cout<<num[i]<<' ';*/
if(n%2==1)
sum=num[(n-1)/2];
else
sum=(double)num[n/2]/2+(double)num[n/2-1]/2;
printf("%.1lf",sum);
return 0;
}