pku 1265 Area

http://poj.org/problem?id=1265

求多面形面积加pick 公式

F为平面上以格子点为定点的单纯多边形,则其面积为:

S=b/2+i-1

b为多边形边上点格点的个数,i为多边形内部格点的个数。

可用其计算多边形的面积,边界格点数或内部格点数。

它明明说:

Separate the three numbers by two single blanks

打两个空格硬是PE。一个才让AC

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <algorithm>
using namespace std;
template<class T>inline string toStr(const T& v){ostringstream os;os<<v;return os.str();}
#define FOR(i,s,e) for (int (i)=(s);(i)<(e);i++)
#define DEBUG(a)     printf("%s = %s\n", #a, toStr(a).c_str())
const double eps=1e-8;
template<class T>inline T isqr(T v) {return v*v;}
template<class T>inline int isgn(T v) {return (v>eps) - (v<-eps);}
const double pi=acos(-1.0);

struct Point2D {
        double x, y;
        Point2D():x(0), y(0){}
        Point2D(const double& X, const double& Y):x(X), y(Y){}
        void rotate(double alpha) {
                double tx=x*cos(alpha)-y*sin(alpha);
                double ty=y*cos(alpha)+x*sin(alpha);
                x=tx; y=ty;
        }
        Point2D operator +(const Point2D& v) const {return Point2D(x+v.x, y+v.y);}
};


int gcd(int a,int b)
{
    if (a < 0)
    a *=-1;
    if (b < 0)
    b *=-1;
    if (a < b )
    return gcd(b,a);
    int c =0;
    while (b)
    {
        c = a % b;
        a = b;
        b = c;
    }
    return a;
}
double area_of_polygon(int vcount,Point2D polygon[])
{
int i;
double s;
if (vcount<3)
return 0;
s=polygon[0].y*(polygon[vcount-1].x-polygon[1].x);
for (i=1;i<vcount;i++)
s+=polygon[i].y*(polygon[(i-1)].x-polygon[(i+1)%vcount].x);
return s/2;
}
Point2D p[1000];
int main()
{
// freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    int n;
    double max,r;
    int num = 1;
    while (t--)
    {
        scanf("%d",&n);
        Point2D pos(0,0);
        FOR(i,0,n)
        {
         scanf("%lf%lf",&p[i].x,&p[i].y);
         p[i] = p[i]+ pos;
         pos = p[i];
        }
        double area = area_of_polygon(n,p);
        printf("Scenario #%d:\n",num++);
        int sum = 0;
        FOR(i,0,n)
        sum+=gcd((int)(p[i].x-p[(i+1)%n].x),(int)(p[i].y-p[(i+1)%n].y));

        double k = area + 1.0 - sum/2.0;
        printf("%.0lf %d %.1lf\n",k,sum,area);
        if (t != 0)
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值