TOJ 3502: Triangle (pick定理)

3502: Triangle 分享至QQ空间 去爱问答提问或回答

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 187            测试通过:92

描述

lattice point is an ordered pair (xy) 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).

输入

The input test file will contain multiple test cases. Each input test case consists of six

 integers x1y1x2y2x3, and y3, where (x1y1), (x2y2), and (x3y3

are the coordinates of vertices of the triangle. All triangles in the input will be 

non-degenerate (will have positive area), and −15000 ≤ x1y1x2y2x3y3 ≤ 15000. 

The end-of-file is marked by a test case with x1 =  y1 = x2 = y2 = x3 = y3 = 0 and

 should not be processed.

输出

For each input case, the program should print the number of internal

 lattice points on a single line.

样例输入

0 0 1 0 0 1
0 0 5 0 0 5
0 0 0 0 0 0

样例输出

0
6

题目来源

SLPC 2004






#include<stdio.h>
#include<math.h>
struct Point 
{
	int x, y;
}p[3];
int gcd(int a, int b)//返回最大公约数 
{
	return b?gcd(b, a%b):a;
}
int mult(Point a, Point b, Point c)//叉乘得面积 
{
	return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}
int main()
{
	int i, j, flag, dx, dy, on;
	double area;
	while(1)
	{
		flag = 0;
		on = 0;
		for(i = 0; i < 3; i++)
		{
			scanf("%d %d",&p[i].x, &p[i].y);
			if(p[i].x || p[i].y)//判断是否都为0,都为0的话break 
				flag = 1;
		}
		if(!flag)
			break;
		for(i = 0; i < 3; i++)//计算各边的点数 
		{
			dx = p[i].x - p[(i+1)%3].x;
			dy = p[i].y - p[(i+1)%3].y;
			on+= gcd(fabs(dx), fabs(dy));
		}
		area = fabs(mult(p[0], p[1], p[2])) / 2.0;
		printf("%.0f\n", area - on / 2.0 + 1);//pick公式 面积 = 内部点数 + 边上点数/2 - 1;所以 in = area - on / 2.0 + 1 
	}
}


猜你喜欢

转载自blog.csdn.net/u013780740/article/details/38704757
今日推荐