HDU1115&&POJ1385Lifting the Stone(求多边形的重心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1115#

大意:给你个n,有n个点,然后给你n个点的坐标,求这n个点形成的多边形的重心的坐标。

直接套模板,我也不知道什么意思。注意在POJ上面定义double时,输出f,如果输出lf则WA,HDU上面输出lf能A。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-9
typedef long long ll;
using namespace std;
int n;
struct Point
{
    double x,y;
} q[1000010];
double cross(Point a,Point b)
{
    return a.x*b.y-a.y*b.x;
}
Point interity(Point p[],int n)
{
    Point inter= {0,0};
    double area=0;
    for(int i=0; i<n; i++)
    {
        double tmp=cross(p[i],p[(i+1)%n]);
        inter.x+=tmp*(p[i].x+p[(i+1)%n].x);
        inter.y+=tmp*(p[i].y+p[(i+1)%n].y);
        area+=tmp;
    }
    inter.x/=3*area;
    inter.y/=3*area;
    return inter;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&q[i].x,&q[i].y);
        }
        Point temp=interity(q,n);
        printf("%.2f %.2f\n",temp.x,temp.y);
    }
    return 0;
}

 

posted @ 2015-02-06 19:55  人艰不拆_zmc  阅读(210)  评论(0编辑  收藏  举报