杭电OJ 1173(C++)

基础题。基地的X坐标为所有点X坐标的中位数,Y坐标为所有点Y坐标的中位数。

#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
const int MAXN = 1000005;

double x[MAXN];
double y[MAXN];

int main()
{
    int n;
    double baseX, baseY;
    while (cin >> n)
    {
        if (n == 0)
            break;
        for (int i = 0; i < n; i++)
        {
            cin >> x[i] >> y[i];
        }
        sort(x, x + n);
        sort(y, y + n);
        if (n % 2 == 0) //n为偶数,取两个中位数的平均数
        {
            baseX = (x[n / 2] + x[n / 2 - 1]) / 2.0;
            baseY = (y[n / 2] + y[n / 2 - 1]) / 2.0;
        }
        else //n为奇数,取中位数
        {
            baseX = x[n / 2];
            baseY = y[n / 2];
        }
        cout << fixed << setprecision(2) << baseX << " " << baseY << endl;
    }
    return 0;
}

继续加油。

发布了138 篇原创文章 · 获赞 1 · 访问量 7027

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/104542603