题目链接:http://acm.hdu.edu.cn/showPRoblem.php?pid=1115 题意:给你一个多边形,让你求他的重心 解析:多边形重心公式,即把多边形分成多个三角形,然后是三角形的重心的权重和
#include <cmath>#include <algorithm>#include <iostream>#include <cstdio>#include <vector>#include <cstring>using namespace std;const int maxn = 1000000+10;const double eps = 1e-5;struct point{ double x; double y; point() {} point(double _x,double _y) { x = _x; y = _y; }}a[maxn];double x_mul(point p0,point p1,point p2){ return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}point gravity(int n){ point res = {0,0}; double tmp = 0; for(int i=0;i<n;i++) { double s = x_mul(point(0,0),a[i],a[(i+1)%n]); tmp += s; res.x += s*(a[i].x+a[(i+1)%n].x); res.y += s*(a[i].y+a[(i+1)%n].y); } res.x = res.x/tmp/3.0; res.y = res.y/tmp/3.0; return res;}int main(void){ int n,t; cin>>t; while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf %lf",&a[i].x,&a[i].y); point ans = gravity(n); printf("%.2f %.2f/n",ans.x,ans.y); } return 0;}新闻热点
疑难解答