Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5909 | Accepted: 2543 |
Description
A lattice point is an ordered pair (x, y) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lattice points), you are to count the number of lattice points which lie completely inside of the triangle (points on the edges or vertices of the triangle do not count).
Input
The input test file will contain multiple test cases. Each input test case consists of six integers x1, y1, x2, y2, x3, and y3, where (x1, y1), (x2, y2), and (x3, y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1, y1, x2, y2, x3, y3 ≤ 15000. The end-of-file is marked by a test case with x1 = y1 = x2 = y2 = x3 = y3 = 0 and should not be processed.
Output
For each input case, the program should print the number of internal lattice points on a single line.
Sample Input
0 0 1 0 0 1 0 0 5 0 0 5 0 0 0 0 0 0
Sample Output
06
题目大意:给你6个点,代表了三角形的三个点的坐标,要求三角形内有多少整点
由皮克定理可以知道:S=A+B/2-1(A为三角形内的整点数,B为三角形边上的整点数,S为三角形的面积)
已知三角形的三点为(x1,y1),(x2,y2),(x3,y3)
则三角形的面积为S=((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/2;
一条直线((0,0),(m,n))上的格点数为gcd(m,n)(gcd为求最大公约数函数)
所以代码如下:
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> using namespace std; int gcd(int a,int b) { return !b?a:gcd(b,a%b); } int main() { int x1,x2,x3,y1,y2,y3; int a,b,area; while(scanf("%d%d%d%d%d%d",&x1, &y1, &x2, &y2, &x3, &y3) != EOF) { if(!x1 && !y1 && !x2 && !y2 && !x3 && !y3) break; area = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1); int p1=gcd(abs(x1-x2),abs(y1-y2)); int p2=gcd(abs(x2-x3),abs(y2-y3)); int p3=gcd(abs(x1-x3),abs(y1-y3)); b=p1+p2+p3; area=abs(area); a=(area+2-b)/2; printf("%d\n",a); } return 0; }
题目链接:点击打开链接http://poj.org/problem?id=2954