P1183 多边形的面积
多边形求面积公式:
$frac {sum_{i=0}^{n-1}(x_iy_{i+1}-y_ix_{i+1})}{2}$
#include<bits/stdc++.h> #define N 105 using namespace std; double x[N],y[N],ans; int n; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); x[0]=x[n],y[0]=y[n]; for(int i=0;i<n;i++){ ans+=0.5*(x[i]*y[i+1]-y[i]*x[i+1]); } printf("%d ",int(ans)); return 0; }
双倍经验:
P2785 物理1(phsic1)- 磁通量
多边形面积公式:
$frac {sum_{i=0}^{n-1}(x_iy_{i+1}-y_ix_{i+1})}{2}$
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; double lastx,lasty,x,y,b,S,stx,sty; int n; int main() { scanf("%d%lf",&n,&b); scanf("%lf%lf",&stx,&sty); lastx=stx,lasty=sty; for(int i=2;i<=n;i++){ scanf("%lf%lf",&x,&y); S+=x*lasty-y*lastx; lastx=x,lasty=y; } S+=lasty*stx-lastx*sty; printf("%.4lf ",fabs(S*b/2.0)); return 0; }