HDU 1115 Lifting the Stone (求多边形的重心)

题目链接:传送门

分析:

求多边形的重心的方法:传送门

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1e-10;

struct Point{
    double x,y;
    Point():x(0),y(0){}
    Point(double _x,double _y):x(_x),y(_y){}
};

//判断符号,提高精度
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    else return x < 0 ? -1 : 1;
}

//叉积,可以用来判断方向和求面积
double cross(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y) - (b.x-c.x)*(a.y-c.y);
}

//求多边形的面积
double S(Point p[],int n){
    double ans = 0;
    for(int i=0;i<n;i++){
        ans+=cross(p[i],p[(i+1)%n],p[(i+2)%n]);
    }
    if(ans<0) ans = -ans;
    return ans/2.0;
}

//求多边形的中心

Point grabity(Point p[],int n){
    Point G;
    double sum_area=0;
    for(int i=2;i<n;i++){
        double area = cross(p[0],p[i-1],p[i]);
        sum_area+=area;
        G.x+=(p[0].x+p[i-1].x+p[i].x)*area;
        G.y+=(p[0].y+p[i-1].y+p[i].y)*area;
    }
    G.x=G.x/3/sum_area,G.y=G.y/3/sum_area;
    return G;
}

Point p[1000010];

int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        Point G = grabity(p,n);
        printf("%.2lf %.2lf\n",G.x,G.y);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-23 10:26:43

HDU 1115 Lifting the Stone (求多边形的重心)的相关文章

hdu 1115 Lifting the Stone (数学几何)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5203    Accepted Submission(s): 2155 Problem Description There are many secret openings in the floor which are covered by a big

Lifting the Stone(hdu1115)多边形的重心

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5370 Accepted Submission(s): 2239 Problem Description There are many secret openings in the floor which are covered by a big heavy

HDU 1115 Lifting the Stone

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5440    Accepted Submission(s): 2278 Problem Description There are many secret openings in the floor which are covered by a big

(hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168 Accepted Submission(s): 98   Problem Description There are many secret openings in the floor which are covered by a big he

HDOJ 1115 Lifting the Stone 多边形重心

来自:http://blog.csdn.net/tiaotiaoyly/article/details/2087498 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X = ∑( xi×mi ) / ∑mi Y = ∑( yi×mi ) / ∑mi 特殊地,若每个点的质量相同,则 X = ∑xi  / n Y = ∑yi  / n 2,质量分布均匀.这个题就是这一类型,算法和上面的不同. 特殊地,质量均匀的三角形重心: X = ( x0 + x1 + x2 ) / 3

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 <algorith

hdoj-1115-Lifting the Stone 求多边形重心问题

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6329 Accepted Submission(s): 2624 Problem Description There are many secret openings in the floor which are covered by a big heavy

Lifting the Stone(求任意多边形的重心)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5432    Accepted Submission(s): 2273 Problem Description There are many secret openings in the floor which are covered by a big

ACM-计算几何之Lifting the Stone——hdu1115

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5026    Accepted Submission(s): 2102 Problem Description There are many secret openings in the floor which are covered by a big