HDU 1115 Lifting the Stone

http://acm.hdu.edu.cn/showproblem.php?pid=1115

题意:求凸多边形的重心

思路:明明是SB题,做法就是三角剖分每个三角形的重心乘上三角形面积的矢量和,最后除以总面积,我愣是WA了3天,最后才知道要把所有的除数放到最后除才不会有精度误差,舞草

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #define dou double
 7 struct Point{
 8     dou x,y;
 9     Point(){}
10     Point(dou x0,dou y0):x(x0),y(y0){}
11 }p[5000005];
12 int n;
13 int read(){
14     int t=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
16     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
17     return t*f;
18 }
19 Point operator +(Point p1,Point p2){
20     return Point(p1.x+p2.x,p1.y+p2.y);
21 }
22 dou operator *(Point p1,Point p2){
23     return p1.x*p2.y-p1.y*p2.x;
24 }
25 Point operator *(Point p1,dou x){
26     return Point(p1.x*x,p1.y*x);
27 }
28 Point operator /(Point p1,dou x){
29     return Point(p1.x/x,p1.y/x);
30 }
31 Point operator -(Point p1,Point p2){
32     return Point(p1.x-p2.x,p1.y-p2.y);
33 }
34 dou area(Point p1,Point p2,Point p3){
35     return (p2-p1)*(p3-p1);
36 }
37 int main(){
38     int T=read();
39     while (T--){
40         n=read();
41         dou S=0;Point ans(0,0);
42         for (int i=1;i<=n;i++)
43           scanf("%lf%lf",&p[i].x,&p[i].y);
44         p[n+1]=p[1];
45         for (int i=2;i<n;i++){
46             S+=((p[i]-p[1])*(p[i+1]-p[1]));
47             ans=ans+((p[i]+p[i+1]+p[1]))*((p[i]-p[1])*(p[i+1]-p[1]));
48         } 
49         ans=ans/(3.0*S);
50         double Ansx,Ansy;
51         Ansx=(double)(ans.x);
52         Ansy=(double)(ans.y);
53         printf("%.2f %.2f\n",Ansx,Ansy);
54     }
55 }

 

posted @ 2016-07-09 23:37  GFY  阅读(219)  评论(0编辑  收藏  举报