poj2954 Triangle
2013-08-27 19:51
232 查看
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
Sample Output
题目大意:就是简单的求一个三角形内的点的个数,其中不包含边上的点。
题解:
需要知道一个叫PICK的定理:
三角形面积s 三角形内部点 n 三角形边上的点 w
n + w/2 - 1 = s
知道三角形顶点求面积很好做,代码里用了最笨的海伦公式
然后求边上的整点数(就是线段上的最大公约数)
代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack>
using namespace std;
struct point {
int x,y;
};
int gcd(int m,int n) {
if(n==0) return m;
return gcd(n,m%n);
}
int main() {
point a[5];
while(1) {
int sum=0;
int i;
for(i=1; i<=3; i++) {
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].x==0&&a[i].y==0)
sum++;
}
if(sum==3)
break;
if((a[2].y-a[1].y)*(a[3].x-a[2].x)==(a[3].y-a[2].y)*(a[2].x-a[1].x)) {
printf("0\n");
continue;
}
int ss=abs( (a[2].x-a[1].x)*(a[3].y-a[1].y)-(a[3].x-a[1].x)*(a[2].y-a[1].y) );
int b=gcd(abs(a[2].x-a[1].x),abs(a[2].y-a[1].y))+gcd(abs(a[3].x-a[2].x),abs(a[3].y-a[2].y))+gcd(abs(a[1].x-a[3].x),abs(a[1].y-a[3].y));
printf("%d\n",(ss-b)/2+1);
}
return 0;
}
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
题目大意:就是简单的求一个三角形内的点的个数,其中不包含边上的点。
题解:
需要知道一个叫PICK的定理:
三角形面积s 三角形内部点 n 三角形边上的点 w
n + w/2 - 1 = s
知道三角形顶点求面积很好做,代码里用了最笨的海伦公式
然后求边上的整点数(就是线段上的最大公约数)
代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack>
using namespace std;
struct point {
int x,y;
};
int gcd(int m,int n) {
if(n==0) return m;
return gcd(n,m%n);
}
int main() {
point a[5];
while(1) {
int sum=0;
int i;
for(i=1; i<=3; i++) {
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].x==0&&a[i].y==0)
sum++;
}
if(sum==3)
break;
if((a[2].y-a[1].y)*(a[3].x-a[2].x)==(a[3].y-a[2].y)*(a[2].x-a[1].x)) {
printf("0\n");
continue;
}
int ss=abs( (a[2].x-a[1].x)*(a[3].y-a[1].y)-(a[3].x-a[1].x)*(a[2].y-a[1].y) );
int b=gcd(abs(a[2].x-a[1].x),abs(a[2].y-a[1].y))+gcd(abs(a[3].x-a[2].x),abs(a[3].y-a[2].y))+gcd(abs(a[1].x-a[3].x),abs(a[1].y-a[3].y));
printf("%d\n",(ss-b)/2+1);
}
return 0;
}
相关文章推荐
- refereed to as “triangle
- android画等边三角形
- 使用Android OpenGL ES 2.0绘图之二:定义形状
- 北大ACM1163 - The Triangle (枚举法&备忘录法&动态规划)
- hdu4466 Triangle
- The Rascal Triangle
- [Algorithms] triangle
- leetcode_120 Triangle_动态规划
- QT Demo 之 threading(3) triangle
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
- C++输出倒三角加数字
- HDU 5914 Triangle (2016-ccpc-长春)
- 动态规划----Triangle
- Triangle
- HDU 5914 Triangle【找规律】【2016中国大学生程序设计竞赛(长春)】
- LeetCode----Triangle
- 利用css的before和after属性,实现类似QQ对话框效果
- leetcode_question_120 Triangle
- triangle
- Triangle