首页 > 其他 > 详细

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

时间:2015-02-06 20:17:49      阅读:419      评论:0      收藏:0      [点我收藏+]

题目链接: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;
}

 

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

原文:http://www.cnblogs.com/zhangmingcheng/p/4277919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!