题解 | #中位数#
中位数
https://www.nowcoder.com/practice/2364ff2463984f09904170cf6f67f69a
#include<cstdio> #include<algorithm> using namespace std; int a[1000]; bool comp(int lhs, int rhs){ if (lhs > rhs){ return true; } else{ return false; } } int main(){ int n; while (scanf("%d", &n) != EOF){ if (0 == n){ break; } for (int i = 0; i < n; i++){ int o; scanf("%d", &a[i]); } sort(a, a+1000,comp); if (n % 2 == 1){ printf("%d\n", a[n / 2]); } else{ printf("%d\n", (a[n / 2] + a[n / 2 - 1]) / 2); } } }