给定
顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积S和内部格点数目n、边上格点数目s的关系:
(其中n表示多边形内部的点数,s表示多边形边界上的点数,S表示多边形的面积)
#include <stdio.h>
struct node
{
int x,y;
} point[110];
int n;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int Area(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
int main()
{
int T,case1=0;
scanf("%d",&T);
while(T--)
{
case1++;
int a=0,p=0,dx,dy,i;
scanf("%d",&n);
point[0].x=0;
point[0].y=0;
for(i=1; i<=n; i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
dx=point[i].x;
if(dx<0)dx=-dx;
dy=point[i].y;
if(dy<0)dy=-dy;
p+=gcd(dx,dy);
point[i].x+=point[i-1].x;
point[i].y+=point[i-1].y;
a+=Area(point[i],point[i-1]);
}
if(a<0)a=-a;
printf("Scenario #%d:\n",case1);
printf("%d %d %.1f\n\n",(a-p+2)/2,p,0.5*a);
}
return 0;
}