题解 | #中位数#
中位数
https://www.nowcoder.com/practice/2364ff2463984f09904170cf6f67f69a
#include <stdio.h> #include <stdlib.h> int cmp(const void* a, const void* b) { return *(int*)a > *(int*)b; } int main() { int n, a[10005]; while (scanf("%d", &n) != EOF) { if (n == 0) break; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } qsort(a, n, sizeof(int), cmp); if (n % 2 == 1) { printf("%d\n", a[(n - 1) / 2]); } else { printf("%d\n", (a[n / 2 - 1] + a[n / 2]) / 2); } } return 0; }