按顺时针或者逆时针顺序输入n个点,求输入点围城的多边形的面积。凸凹都可以计算。
模板
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <fstream> 8 #include <cstdio> 9 #include <cmath> 10 #include <stack> 11 #include <queue> 12 using namespace std; 13 const double Pi=3.14159265358979323846; 14 typedef long long ll; 15 const int MAXN=5000+5; 16 const int dx[5]={0,0,0,1,-1}; 17 const int dy[5]={1,-1,0,0,0}; 18 const int INF = 0x3f3f3f3f; 19 const int NINF = 0xc0c0c0c0; 20 const ll mod=1e9+7; 21 struct point{ 22 double x;double y; 23 point (double xx,double yy):x(xx),y(yy) {} 24 }; 25 26 double ComAre(const vector <point> &V) 27 { 28 int len=V.size(); 29 if(len<3) return 0.0; 30 double s=0; 31 for(int i=0;i<len;i++) 32 { 33 s+=V[i].x*V[(i+1)%len].y-V[i].y*V[(i+1)%len].x; 34 } 35 return fabs(s/2); 36 } 37 38 int main() 39 { 40 int n; 41 while(cin>>n&&n) 42 { 43 vector <point> V; 44 for(int i=1;i<=n;i++) 45 { 46 double a,b;cin>>a>>b; 47 V.push_back(point(a,b)); 48 } 49 double ans=ComAre(V); 50 printf("%.1lf\n",ans); 51 } 52 return 0; 53 }
原文:https://www.cnblogs.com/Msmw/p/10747772.html