Blast the Enemy!

题目链接

[UVALive 4426] Blast the Enemy!

题解

中档题。题目看起来很复杂实质上是求多边形的中心。
模板题。

代码

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;

const int MAXN = 105;
int N;
double S[110];
struct Point{
    double x, y;
    Point(){}
    Point(double _x, double _y) {
        x = _x;
        y = _y;
    }
    Point operator + (const Point& b) {
        return Point(x + b.x, y + b.y);
    }
    Point operator - (const Point& b) {
        return Point(x - b.x, y - b.y);
    }
    Point operator * (const double &b) {
        return Point(x*b, y*b);
    }
    Point operator / (const double &b) {
        return Point(x / b, y / b);
    }
}input[MAXN];

double cross(Point a, Point b) {
    return a.x*b.y - a.y*b.x;
}
double area(Point a, Point b, Point c) {
    return cross(b - a, c - a);
}


int main() {
    int T = 1;
    while ((cin >> N) && N) {
        for (int i = 1; i <= N; i++) {
            cin >> input[i].x >> input[i].y;
        }
        S[0] = 0;
        Point temp(0, 0);
        for (int i = 2; i<N; i++) {
            S[i] = area(input[1], input[i], input[i + 1]); S[0] += S[i];
            temp = temp + ((input[1] + input[i] + input[i + 1]) / 3.0)*S[i];
        }
        temp = temp / S[0];
        cout << "Stage #" << T++ << ": ";
        cout << fixed << temp.x << ' ' << fixed << temp.y << endl;
    }
}

标签: math, geometry

添加新评论