HDU 1115
题意很明白要求多边形重心。方法已在上篇讲过了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int MAXN=1000005; struct point { double x,y; }; point p[MAXN]; int n; point operator - (const point &u,const point &v){ point ret; ret.x=u.x-v.x; ret.y=u.y-v.y; return ret; } double operator *(point u,point v){ return u.x*v.y-u.y*v.x; } int main(){ int T; 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 p0,p1,p2; p[n]=p[0]; p0=p[0]; double sum_area=0,sum_x=0,sum_y=0; for(int i=1;i<n;i++){ p1=p[i]; p2=p[i+1]; double V=((p1-p0)*(p2-p0))/2; sum_area+=V; sum_x+=(p0.x+p1.x+p2.x)*V; sum_y+=(p0.y+p1.y+p2.y)*V; } printf("%.2lf %.2lf\n",sum_x/3/sum_area,sum_y/3/sum_area); } return 0; }