zoj1010 Area

1. //zoj1010 Area
2. //Accepted 1010 C++ 00:00.09 412K
3. #include <stdio.h>
4. #include <math.h>
5. #define MAXN 1000
6. struct point{   double x,y; } p[MAXN];
7. int n;
8. double xmult(double x1,double y1,double x2,double y2){return x1*y2-x2*y1;}
9. double xmult3(double x1,double y1,double x2,double y2,double x3,double y3){
10.        return (x1-x3)*(y2-y3)-(x2-x3)*(y1-y3);
11. }
12. //判点p1p2在线段p3p4同侧
13. bool same_side(point p1,point p2,point p3,point p4)
14. {
15.        double dx,dy,dx1,dy1,dx2,dy2;
16.        dx = p4.x-p3.x, dy = p4.y-p3.y;
17.        dx1 = p1.x-p3.x, dy1 = p1.y-p3.y;
18.        dx2 = p2.x-p4.x, dy2 = p2.y-p4.y;
19.        return xmult(dx,dy,dx1,dy1)*xmult(dx,dy,dx2,dy2)>0.0;
20. }
21. //p1p2p3p4相交
22. bool intersect(point p1,point p2,point p3,point p4)
23. {
24.        return (!same_side(p1,p2,p3,p4) && !same_side(p3,p4,p1,p2));
25. }
26.
27. double area_polygon()
28. {
29.        int i;
30.        double s1=0,s2=0;
31.        for (i=0; i<n; ++i){
32.               s1 += p[(i+1)%n].y*p[i].x;
33.               s2 += p[(i+1)%n].y*p[(i+2)%n].x;
34.        }
35.        return fabs(s1-s2)/2;
36. }
37.
38. void solve()
39. {
40.        int i,j,k;
41.        for (i=0; i<n; ++i)
42.               scanf ("%lf %lf",&p[i].x,&p[i].y);
43.        if (n<3){
44.               printf ("Impossible/n");
45.               return ;
46.        }
47.        for (i=0; i<n; ++i)
48.               for (j=(i+1)/n; j<i-1; ++j){
49.                      k = (i+1)%n;
50.                      if (intersect(p[i],p[k],p[j],p[j+1])){
51.                             printf ("Impossible/n");
52.                             return ;
53.                      }
54.               }
55.        printf ("%.2lf/n",area_polygon());
56. }
57.
58. int main()
59. {
60. #ifdef ONLINE_JUDGE
61. #else
62.        freopen("1010.txt","r",stdin);
63. #endif
64.        int t=0;
65.        while (scanf ("%d",&n) != EOF && n){
66.               if (t) printf ("/n");
67.               printf ("Figure %d: ",++t);
68.               solve();
69.        }
70. #ifdef ONLINE_JUDGE
71. #else
72.        fclose(stdin);
73. #endif
74.        return 0;
75. }
• 本文已收录于以下专栏：

举报原因： 您举报文章：zoj1010 Area 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)