#include<iostream>#include<cmath>#include<cstdio>using namespace std;struct Point{ double x,y;};int main(){ int n,t; scanf("%d",&t); while(t--){ scanf("%d",&n); Point p1,p2,p3; double gx,gy,sumarea; gx=gy=sumarea=0; scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y); for(int i=2;i<n;i++){ scanf("%lf%lf",&p3.x,&p3.y); double area=((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))/2; gx+=(p1.x+p2.x+p3.x)*area; gy+=(p1.y+p2.y+p3.y)*area; sumarea+=area; p2=p3; } gx=gx/sumarea/3; gy=gy/sumarea/3; if(fabs(sumarea - 0) < 0.0000001)puts("0.000 0.000"); else printf("%.3lf %.3lf\n",fabs(sumarea),gy+gx); } return 0;}
Code 3 (reference: Click to open the link)
# Include <iostream> # include <cstring> # include <cstdio> # include <cmath> # include <algorithm> using namespace std; # define INF 0.0000001 const int N = 10010; struct point {double x, y; point (): x (0), y (0) {}} p [N]; int main () {int ncase; scanf ("% d", & ncase); while (ncase --) {int n; double result = 0; point ans; scanf ("% d", & n ); for (int I = 0; I <n; ++ I) scanf ("% lf", & p [I]. x, & p [I]. y); for (int I = 1; I <= n; ++ I) {double temp = (p [I % n]. x * p [I-1]. y-p [I % n]. y * p [I-1]. x)/2.0; result + = temp; ans. x + = temp * (p [I % n]. x + p [I-1]. x)/3.0; ans. y + = temp * (p [I % n]. y + p [I-1]. y)/3.0;} if (fabs (result-0) <INF) puts ("0.000 0.000"); elseprintf ("%. 3lf %. 3lf \ n ", fabs (result), (ans. x + ans. y)/result); // result returns 0;}/* 172 34 53 47 84 7834 568 100 */
(Full text)