杭电1065 I Think I Need a Houseboat

/*********************************
 *    日期:2011-3-7
 *    作者:SJF
 *    题号:杭电1065
 *    题目:I Think I Need a Houseboat
 *    结果:AC
 *    总结:有多边形重心公式,需要知道每个顶点坐标,令面积为area,则
             area=sum(X[i]*Y[i+1]-X[i+1]*Y[i])
             x=sum( (X[i]*Y[i+1]-X[i+1]*Y[i]) * (Xi+X(i+1)) / (6*area) )
             y=sum( (X[i]*Y[i+1]-X[i+1]*Y[i]) * (Yi+Y(i+1)) / (6*area) )
             x,y为重心坐标;
**********************************/
#include  
#define MAX 1000010  
typedef struct 
{  
    double x;  
    double y;  
}Point;  
Point point[MAX];
void Gravity(int n)
{
    int i;
    double area=0,x=0,y=0;
    point[n]=point[0];
    for(i=0;i     {
        area+=((point[i].x)*(point[i+1].y)-(point[i+1].x)*(point[i].y))/2;
        x+=((point[i].x)*(point[i+1].y)-(point[i+1].x)*(point[i].y))*(point[i].x+point[i+1].x);
        y+=((point[i].x)*(point[i+1].y)-(point[i+1].x)*(point[i].y))*(point[i].y+point[i+1].y);
    }
    printf("%.2lf %.2lf\n",x/(6*area),y/(6*area));
}
int main()
{
    int T,n;
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            int i;
            scanf("%d",&n);
            for(i=0;i             {
                scanf("%lf %lf",&point[i].x,&point[i].y);
            }
            Gravity(n);
        }
    }
}
    

 

 

 

 

 

 

 

 

 


 

你可能感兴趣的:(OJ)