题解 | #中位数#
中位数
https://www.nowcoder.com/practice/2364ff2463984f09904170cf6f67f69a
//知道sort后这题就简单多了,而且还是O(log2N)的复杂度
#include "stdio.h"
#include "algorithm"
using namespace std;
int main(){
int N;int num[10001];
while (scanf("%d",&N)!=EOF) {
if(N == 0)
return 0;
for (int i =0; i<N; ++i) {
scanf("%d",num+i);
}
sort(num,num+N);
if(N%2 != 0)
printf("%d\n",num[N/2]);
else{
printf("%d\n",(num[N/2]+num[N/2-1])/2);
}
}
}