HDU 1115 Lifting the Stone 多边形重心 模板体

来源:互联网 发布:程序员真的那么累吗 编辑:程序博客网 时间:2024/06/06 18:42
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <cmath>#include <algorithm>using namespace std;struct P{    double x,y;};int main(){    int T,n;    int i,j;    P p1,p2,p3;    double X,Y,S,x,y,s;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y);        for(X = 0,Y = 0,S = 0,i = 3; i <= n; ++i)        {            scanf("%lf %lf",&p3.x,&p3.y);            x = (p1.x + p2.x + p3.x);            y = (p1.y + p2.y + p3.y);            s = ((p3.x - p1.x)*(p2.y - p1.y) - (p2.x - p1.x)*(p3.y - p1.y))/2;            X += x*s;            Y += y*s;            S += s;            p2 = p3;        }        printf("%.2lf %.2lf\n",X/S/3,Y/S/3);    }    return 0;}

原创粉丝点击