计算几何初步——面积公式(POJ 1654)

这题要注意一点就是数据类型的选择。。。

一定要选__int64或者long long,否则很容易出问题。

其余的就是直接用公式。。。。。

View Code
 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #define MAXN 1000004
5 #define END 5
6 using namespace std;
7 __int64 dir[10][2];
8 struct point {
9 __int64 x, y;
10 };
11
12 char data[MAXN];
13 void ini()
14 {
15 dir[7][0] = dir[4][0] = dir[1][0] = -1;
16 dir[9][0] = dir[6][0] = dir[3][0] = 1;
17 dir[2][0] = dir[8][0] = 0;
18 dir[9][1] = dir[8][1] = dir[7][1] = 1;
19 dir[3][1] = dir[2][1] = dir[1][1] = -1;
20 dir[6][1] = dir[4][1] = 0;
21 }
22
23 __int64 cross(point &p, point &q)
24 {
25 return q.y*p.x - p.y*q.x;
26 }
27
28 int main()
29 {
30 int T;
31 ini();
32 scanf("%d",&T);
33 getchar();
34 while (T--) {
35 cin>>data;
36 __int64 sx(0), sy(0);
37 point p1,p2;
38 p2.x = p1.x = sx;
39 p2.y = p1.y = sy;
40 __int64 nowx(0), nowy(0);
41 int len = strlen(data);
42 if (len == 1 || len == 3) {
43 cout<<0<<endl;
44 continue;
45 }
46 __int64 area(0);
47 for (int i(0),j(1); i<len-3; ++i) {
48 j = i + 1;
49 nowx += dir[data[i] - '0'][0];
50 nowy += dir[data[i] - '0'][1];
51 p1.x = nowx;
52 p1.y = nowy;
53 p2.x = nowx + dir[data[j] - '0'][0];
54 p2.y = nowy + dir[data[j] - '0'][1];
55 area += cross(p1,p2);
56 }
57 if (area < 0)area = -area;
58 if(area % 2)printf("%I64d.5\n", area/2);
59 else printf("%I64d\n", area/2);
60 }
61 return 0;
62 }



posted on 2012-01-17 14:57  Dev-T  阅读(256)  评论(0编辑  收藏  举报