|
Triangle
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 0 6 Source |
题目:http://poj.org/problem?id=2954
题意:给你个三角形,要求在三角形内的整数点的个数
分析:poj 1265的简化版吧,套用公式就过了
代码:
#include<cstdio>
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
if(a<0)a=-a;
if(b<0)b=-b;
int c;
while(b)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int main()
{
int x1,y1,x2,y2,x3,y3,area,line;
while(~scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3),x1|y1|x2|y2|x3|y3)
{
area=((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2;
if(area<0)area=-area;
line=gcd(x1-x2,y1-y2)+gcd(x1-x3,y1-y3)+gcd(x2-x3,y2-y3);
printf("%d\n",area+1-line/2);
}
return 0;
}
本篇介绍了一个计算给定三角形内部整数点数量的问题,输入为三角形顶点坐标,输出为完全位于三角形内的整数点数量。通过公式计算,避免了逐点检查的方法。

被折叠的 条评论
为什么被折叠?



