题意:就是给你一个多边行的点的坐标,求此多边形的重心。
一道求多边形重心的模板题!
#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=pnt[0].x;
p.y=pnt[0].y;
for(int i=1;i<=n;++i){
s.x=pnt[(i==n)?0:i].x;
s.y=pnt[(i==n)?0:i].y;
tp=(p.x*s.y-s.x*p.y);
area += tp/2;
tpx += (p.x + s.x) * tp;
tpy += (p.y + s.y) * tp;
p.x = s.x; p.y = s.y;
}
s.x=tpx / (6*area);s.y=tpy/(6*area);
return s;
}
int main()
{
int N,t;
scanf("%d",&t);
while(t--){
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%lf%lf",&PP[i].x,&PP[i].y);
}
point ss = bcenter(PP,N);
printf("%.2lf %.2lf\n",ss.x,ss.y);
}
return 0;
}