Hangzhou Electric OJ 1173 (C ++)

Basic questions. The median for all X coordinates of the base point X coordinate of the median, Y coordinates for all points Y coordinates.

#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;
}

Keep up.

Published 138 original articles · won praise 1 · views 7027

Guess you like

Origin blog.csdn.net/Intelligence1028/article/details/104542603