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;
}
时间: 2024-10-12 14:52:42

HDU1115&&POJ1385Lifting the Stone(求多边形的重心)的相关文章

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

(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 求多边形重心问题

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

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

hdu3685 Rotational Painting 求多边形重心和凸包

http://acm.hdu.edu.cn/showproblem.php?pid=3685 Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2614    Accepted Submission(s): 737 Problem Description Josh Lyman is a gifted

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

UVALive 4426 Blast the Enemy! --求多边形重心

题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define Mod 100000000