poj2388 who is in the middle

//题意:给定一组数,求中位数

//思路:直接快排

#include <iostream>
#include <algorithm>

using namespace std;

bool cmp(const int a, const int b)
{
    return a < b;
}

int main()
{
    ios :: sync_with_stdio(false);
    cin.tie(0);
    int t;
    while(cin >> t){
        int *n = new int [t];
        for(int i = 0; i < t; i++)
            cin >> n[i];
        sort(n, n+t, cmp);
        if(t & 1){
            cout << n[t/2] << endl;
        }else
            cout << n[t/2-1] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/small__snail__5/article/details/80174615
今日推荐