转自:http://hi.去掉baidu.com/tianzhang去掉0000/item/1f7617ba去掉567c1f5dba0e12ad
(去掉 “去掉”)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct point//点
{
double x;
double y;
};
double Area(point p0,point p1,point p2)//运用数学知识
{
double area=0;
area=((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));//三角形面积公式--s=((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/2;
return area/2;
}
int main()
{
int tcase;
int n;
point p0,p1,p2;//记录坐标
double sum_area;//面积和
double area;//三角形面积
double sum_x;//x坐标和
double sum_y;//y坐标和
scanf("%d",&tcase);
while(tcase--)
{
sum_area=sum_x=sum_y=0;//初始化
scanf("%d",&n);
scanf("%lf%lf",&p0.x,&p0.y);//先输入两个点的坐标把它作为p0,p1
scanf("%lf%lf",&p1.x,&p1.y);
for(int i=2;i<n;i++)
{
scanf("%lf%lf",&p2.x,&p2.y);
area=Area(p0,p1,p2);
sum_area+=area;//面积和
sum_x+=(p0.x+p1.x+p2.x)*area;//(x轴坐标*面积)
sum_y+=(p0.y+p1.y+p2.y)*area;//(y轴坐标*面积)
p1=p2;//通过这部,达到循环的目的,一直往后面走
}
printf("%.2f %.2f\n",sum_x/(3*sum_area),sum_y/(3*sum_area));//重心公式
}
return 0;
}