原题

多组数据,到0为止。
每次给出按顺序的n个点(可能逆时针,可能顺时针),求多边形面积(保留整数)


多边形面积为依次每条边(向量)叉积/2的和
\(S=\sum _{i=1}^{n-1}p[i]*p[i+1]/2\)
//逆时针为正,顺时针为负

#include<cstdio>
#define N 1010
using namespace std;
int n;
struct hhh
{
    double x,y;
    hhh() {}
    hhh(double _x,double _y) : x(_x),y(_y) {}
    double operator * (const hhh &b) const
	{
	    return x*b.y-b.x*y;
	}
}p[N];

double Area()
{
    double ans=0;
    for (int i=2;i<=n;i++)
	ans+=(p[i-1]*p[i])/2;
    ans+=(p[n]*p[1])/2;
    return ans;
}

double abs(double x) { return x>=0?x:-x; }

int main()
{
    while (~scanf("%d",&n) && n)
    {
	for (int i=1;i<=n;i++)
	    scanf("%lf%lf",&p[i].x,&p[i].y);
	printf("%.f\n",abs(Area()));
    }
    return 0;
}

相关文章:

  • 2021-07-13
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2021-09-21
  • 2022-02-03
  • 2021-06-26
  • 2021-08-08
猜你喜欢
  • 2021-12-31
  • 2021-07-16
  • 2021-12-09
  • 2022-12-23
  • 2021-06-05
  • 2022-03-09
  • 2021-12-06
相关资源
相似解决方案