题解 | 中位数
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<int>ans; while(cin>>n) { if(n==0)break; ans.clear(); while(n--){ int x;cin>>x; ans.push_back(x); } sort(ans.begin(),ans.end()); int k=ans.size(); if(k%2==0) { cout<<(ans[k/2]+ans[k/2-1])/2<<endl; } else cout<<ans[k/2]<<endl; } }
直接写一个sort完事