hdu1115【多边形求重心模板】

1.质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心(∑( xi×mi ) / ∑mi, ∑( yi×mi ) / ∑mi)

2.质量分布均匀。这个题就是这一类型,算法和上面的不同。

特殊地,质量均匀的三角形重心:(( x0 + x1 + x2 ) / 3,Y = ( y0 + y1 + y2 ) / 3)

以(0,0)为顶点三角剖分之后求三角形重心,把重心连起来转换成质量集中在顶点上的情况求解即可

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int N=1000005;
int T,n;
double am;
struct dian
{
    double x,y,v;
    dian(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    dian operator + (const dian &a) const
    {
        return dian(x+a.x,y+a.y);
    }
    dian operator - (const dian &a) const
    {
        return dian(x-a.x,y-a.y);
    }
    dian operator * (const double &a) const
    {
        return dian(x*a,y*a);
    }
    dian operator / (const double &a) const
    {
        return dian(x/a,y/a);
    }
}p[N],a;
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>‘9‘||p<‘0‘)
    {
        if(p==‘-‘)
            f=-1;
        p=getchar();
    }
    while(p>=‘0‘&&p<=‘9‘)
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
double cj(dian a,dian b)
{
    return a.x*b.y-a.y*b.x;
}
int main()
{
    T=read();
    while(T--)
    {
        n=read();am=0,a.x=0,a.y=0;
        for(int i=1;i<=n;i++)
            p[i].x=read(),p[i].y=read();
        p[n+1]=p[1];
        for(int i=2;i<=n+1;i++)
        {
            double mj=cj(p[i-1],p[i]);
            am+=mj,a=a+(p[i-1]+p[i])*mj;
        }
        printf("%.2f %.2f\n",a.x/am/3,a.y/am/3);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lokiii/p/8503190.html

时间: 2024-08-14 23:31:53

hdu1115【多边形求重心模板】的相关文章

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

hdu1115 Lifting the Stone(几何,求多边形重心模板题)

题意:就是给你一个多边行的点的坐标,求此多边形的重心. 一道求多边形重心的模板题! #include<cstdio> #include<cmath> #include<cstring> using namespace std; struct point { double x,y; }PP[1000047]; point bcenter(point pnt[],int n){ point p,s; double tp,area = 0, tpx=0, tpy=0; p.x

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

hdu1115(多边形重心算法)

题目意思: 给出一个n边形的n个顶点,求出这个n边形的重心坐标. http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目分析: /** *出处:http://blog.csdn.net/ysc504/article/details/8812339 *①质量集中在顶点上 *  n个顶点坐标为(xi,yi),质量为mi,则重心 * X = ∑( xi×mi ) / ∑mi * Y = ∑( yi×mi ) / ∑mi * 特殊地,若每个点的质量相同,则 *

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

hdu1115(计算多边形几何重心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1115 题意:给出一些点,求这些点围成的多边形的重心: 思路: 方法1:直接分别求所有点的x坐标的平均值和y坐标的平均值,即答案:不过这个方法的计算精度不是很高,要求高精度时用另一个方法: 方法2: 用公式:x = (xi*si*+...xn*sn)/(si+...+sn): y = (yi*si*+...yn*sn)/(si+...+sn): 方法2的代码: 1 #include <iostream

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){} Poi

zoj 1081 Points Within 判断点是否在任意多边形内(模板)

题目来源: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81 分析: 从p点出发做平行于x轴的射线 l. 求射线与 多边形 线段的交点数num, 若是偶数 , 该点 在外, 若为奇数, 该点在内. 注意: 两个特判, 1:   一个是 射线 l 与 多边形的边  重合 , 若该p点在 线段上, 返回1, 否则 交点 记为 0 个 2: 一个是 射线与 线段的交点 ,为线段的端点, 则我们 对线段的 较低交点 不计算. 代码

多边形相交面积模板

1 /* 2 类型:多边形相交面积模板 3 */ 4 5 #include<cstdio> 6 #include<iostream> 7 #include<algorithm> 8 #include<cstring> 9 #include<cmath> 10 using namespace std; 11 #define maxn 510 12 const double eps=1E-8; 13 int sig(double d){ 14 ret