您的位置:首页 > 产品设计 > UI/UE

POJ 3907 Build Your Home | 计算多边形面积

2017-12-07 16:08 393 查看

给个多边形

计算面积

输出要四舍五入

直接用向量叉乘就好

四舍五入可以+0.5向下取整

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 10005
#define eps 1e-8
using namespace std;
struct point
{
double x,y;
inline double operator *(const point &rhs) const
{
return x*rhs.y-y*rhs.x;
}
}po
;
int n;
double S;
double abs(double x)
{
return x>0?x:-x;
}
double calc()
{
double ret=0;
for (int i=1;i<=n;i++)
ret+=po[i]*po[i+1];
return abs(ret/2)+0.5;
}
int main()
{
while (scanf("%d",&n)!=EOF && n)
{
for (int i=1;i<=n;i++)
scanf("%lf%lf",&po[i].x,&po[i].y);
po[n+1]=po[1];
printf("%d\n",(int)calc());
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: