Sunday, December 10, 2017

UVa problem solution 10107 - What is the Median?

problem link:

Discuss: in this problem you can simply use vector and solve it. there is no tricky part in this problem. all things are describe fully

try yourself before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,j,n,h,sum,m;
    vector<long long>vec;
    while(cin >>n)
    {
        vec.push_back(n);
        sort(vec.begin(),vec.end());
        m=vec.size();
         int h=m/2;
        if(m%2==0)
        {
            sum=vec[h]+vec[h-1];
            cout <<sum/2<<endl;
        }
        else
        {
            cout <<vec[h]<<endl;
        }
    }
    return 0;
}

No comments:

Post a Comment