POJ 2954 Triangle (pick 定理)

題目大意:給出三個點的座標,問在這三個點座標裏面的整數座標點有多少個(不包含邊上的)ios

匹克定理:I = (A-E) / 2 + 1;  spa

A: 表示多邊形面積   code

I : 表示多邊形內部的點的個數   io

E: 表示在多邊形上的點的個數   class

// Time 0ms; Memory 164K
#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;

typedef struct point 
{
	int x,y;
	point(int xx=0,int yy=0):x(xx),y(yy){}
}vector;

point a,b,c;
vector u,v,w;

vector operator - (point p,point q)
{
	return vector(p.x-q.x,p.y-q.y);
}
int cross(vector p,vector q)
{
	return p.x*q.y-p.y*q.x;
}
int gcd(int x,int y)
{
	static int t;
	for(;t=y;y=x%y,x=t);
	return x;
}
int main()
{
	int i,A,I,E;
	while(scanf("%d%d%d%d%d%d",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y)!=EOF && (a.x || a.y || b.x || b.y || c.x || c.y))
	{
		u=a-b;
		v=b-c;
		w=c-a;
		A=abs(cross(u,v));
		E=abs(gcd(u.x,u.y))+abs(gcd(v.x,v.y))+abs(gcd(w.x,w.y));
		I=(A-E)/2+1;
		printf("%d\n",I);
	}
	return 0;
}
相關文章
相關標籤/搜索