标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22973 Accepted Submission(s): 11889
for i from 1 to n-1
S += (pg.p[i]-pg.p[0])×(pg.p[i+1]-pg.p[0])
return fabs(0.5*S);
下面是代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 #define N 105 6 struct point{ 7 double x ; double y ; 8 point (double x_, double y_) 9 { 10 x = x_; 11 y = y_; 12 } 13 point (){} 14 point operator - (const point a) const 15 { 16 return point(x-a.x,y-a.y); 17 } 18 double operator * (const point a) const 19 { 20 return x*a.y - y*a.x; 21 } 22 }P[N]; 23 int main() 24 { 25 int n ; 26 while(~scanf("%d",&n),n) 27 { 28 for(int i = 0 ;i < n ; i++) 29 { 30 scanf("%lf%lf",&P[i].x,&P[i].y); 31 } 32 double area = 0; 33 point A = P[0]; 34 for(int i = 0 ;i < n-1 ;i++) 35 { 36 point B = P[i]; 37 point C = P[i+1]; 38 area+=0.5*((B-A)*(C-A)); 39 } 40 printf("%.1f\n",area); 41 } 42 return 0; 43 }
其中point 中定义的构造函数是为了后面的运算符重载的方便,而一定要写一个没有参量的空构造函数
标签:
原文地址:http://www.cnblogs.com/shanyr/p/4688131.html