Area POJ - 1654

Area

 POJ - 1654

题意:就是让你求多边形面积

思路:利用多边形面积公式即可,输出格式第一次见。。。wa了好几发。。

 1 // 
 2 // Created by HJYL on 2020/2/3.
 3 //
 4 #include<iostream>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<cmath>
 9 #include<algorithm>
10 using namespace std;
11 const double eps=1e-8;
12 const int maxn=1e6+5;
13 struct Point{
14     int x,y;
15     Point(int x=0,int y=0):x(x),y(y){}
16     Point operator - (Point const &b)const
17     {
18         return Point(x-b.x ,y-b.y);
19     }
20     bool operator < (Point const &c)const{
21         if(x==c.x)
22             return y<c.y;
23         return x<c.x;
24     }
25 }p[maxn];
26 double Cross(Point A,Point B)
27 {
28     return (A.x*B.y)-(A.y*B.x);
29 }
30 long long PolygonArea(Point* p, int n) {//p为端点集合,n为端点个数
31     long long s = 0;
32     for (int i = 1; i < n - 1; ++i)
33         s += Cross(p[i] - p[0], p[i + 1] - p[0]);
34     return s<0?-s:s;
35 }
36 char s[maxn];
37 const int dx[]={0,-1,0,1,-1,0,1,-1,0,1};
38 const int dy[]={0,-1,-1,-1,0,0,0,1,1,1};
39 int n;
40 int main()
41 {
42     while(~scanf("%d",&n))
43     {
44         while(n--)
45         {
46             getchar();
47             scanf("%s",s);
48             int l=strlen(s);
49             p[0].x=0,p[0].y=0;
50             for(int i=1;i<l;i++) {
51                 p[i].x = p[i - 1].x +dx[s[i]-'0'], p[i].y = p[i - 1].y +dy[s[i]-'0'];
52             }
53             long long ss=PolygonArea(p,l);
54             if(ss%2==0) printf("%lld\n",ss/2);
55             else printf("%lld.5\n",ss/2);
56         }
57     }
58     return 0;
59 }

 

posted @ 2020-02-03 18:33  branna  阅读(107)  评论(0编辑  收藏  举报