UVA10107:

What is the Median?

#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main(){
	
	long n; 
	vector<long> vec;
	vector<long>::iterator it;

	bool first = true;
	while(cin >> n){
		if(first){
			vec.push_back(n);
			first = false;
		} else {	
			it = upper_bound(vec.begin(), vec.end(), n);
			vec.insert(it, n);
		}	

		int n = vec.size();
		int med = n/2;

		if(n % 2 == 1){
			cout << vec[med] << endl;
		} else {
			cout << (vec[med-1] + vec[med])/2 << endl;

		}
		

	}
	
	return 0; 
}
[Back to Listing Page]

Home

Posts

Projects

Github

Contact

janzzen

Developed by Janzzen Ang powered by Vercel