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;
}