poj 1265 pick定理的应用,水

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct point{
	int x,y;
}p[1000];
int gcd(int a,int b)
{
    if(b==0) return a;
	return gcd(b,a%b);
}
int onedge(int n,point *p)//多边形边上的网格点数
{
	int i,ret=0;
	for(i=0;i<n;i++)
		ret+=gcd(abs(p[i].x-p[(i+1)%n].x),abs(p[i].y-p[(i+1)%n].y));
	return ret;
}
double getarea(point *p,int n)
{
	  int i;
	  double area=0;
      for(i=0;i<n;i++) area+=p[(i+1)%n].y*(p[i].x-p[(i+2)%n].x);
	  return area;
}
int inside(int n,point *p)
{
	double area=0;
    area=getarea(p,n);
	return (fabs(area)-onedge(n,p))/2+1;
}
int main()
{
	int t,cases=1,i;
	int n,dx,dy;
	scanf("%d",&t);
    while(t--)
	{
        scanf("%d",&n);
		point a;
		a.x=0;a.y=0;
		p[0]=a;
        for(i=1;i<=n;i++)
		{
			scanf("%d%d",&dx,&dy);
            p[i].x=p[i-1].x+dx;
			p[i].y=p[i-1].y+dy;
		}
	    int I,E;
		double area;
		I=inside(n,p);
		E=onedge(n,p);
		area=getarea(p,n)/2;
    	printf("Scenario #%d:\n%d %d %.1lf\n\n",cases++,I,E,area);
	}
	return 0;
}

posted @ 2011-10-18 12:22  Because Of You  Views(168)  Comments(0Edit  收藏  举报