【題解】ZeroJudge c010: 10107 – What is the Median?

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c010
【解題想法】利用multiset自動排序

#include <iostream>
#include <set>
#include <iterator>
using namespace std;

int main() {
  int n, cnt=0;
  multiset<int> st;
  while (cin >> n) {
    st.insert(n);
    cnt++;
    multiset<int>::iterator it=st.begin();
    advance(it, st.size()/2);
    if (cnt%2==0) {
      cout << (*it + *(--it)) / 2 << endl;
    } else {
      cout << *it << endl;
    }
  }
	return 0;
}
分享本文 Share with friends