NYOJ 3 多边形重心问题

http://acm.nyist.net/JudgeOnline/problem.php?pid=3

刚开始的时候自己没想到,然后看大牛的题解。一直不懂的是为什么大牛用两个点就可以求出面积来。

问了明智,知道了原来是因为所有的面积的计算表达式的展开会消去相同的项,然后就两个点表示就可以了。

大牛的日志:

http://blog.csdn.net/niushuai666/article/details/7454078

View Code
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 const int m = 100005;
 6 const double INF = 1e-8;
 7 struct Point
 8 {
 9     double x,y;
10 }p[m];
11 
12 int main()
13 {
14     int n,t,i,j;
15     double temp,x,y;
16     scanf("%d",&t);
17     while(t--)
18     {
19         double ans=0;
20         x=0; y=0;
21         scanf("%d",&n);
22         for(i=0;i<n;i++)
23         scanf("%lf%lf",&p[i].x,&p[i].y);
24         for(i=1;i<=n;i++)
25         {
26             temp=(p[i%n].x*p[i-1].y-p[i%n].y*p[i-1].x)/2.0;
27             ans+=temp;
28             x+=temp*(p[i%n].x+p[i-1].x)/3.0;
29             y+=temp*(p[i%n].y+p[i-1].y)/3.0;
30         }
31         if(fabs(ans-0)<INF)
32         {
33             printf("0.000 0.000\n");
34         }
35         else
36         {
37             printf("%.3lf %.3lf\n",fabs(ans),(x+y)/ans);
38         }
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/yoru/p/2688383.html