码迷,mamicode.com
首页 > 其他好文 > 详细

皮克定理及其应用

时间:2019-04-16 01:34:05      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:eve   break   main   scanf   open   应用   flag   problem   sed   

H - 三角形

4.1 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).

4.2 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. For example:

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

 

4.3 Output

For each input case, the program should print the number of internal lattice points on a single line. For example:

0
6


思路:该题求的是三角形内部(不包含边界的)的整点,需要运用到的皮克定理。什么是皮克定理呢?
   2*S=2*A+B-2
(S为三角形面积,A为三角形内部的整点数,B为三角形边上整点数)

那么问题来了,三角形面积如何求?海伦公式?并不是,这里需要运用2S=x1y2+x2y3+x3y1-x1y3-x2y1-x3y2

定点数如何求?两点之间坐标相减并求它们的最大公因数:b=∑gcd(|xi-x[(i+1)%3]|,|yi-y[(i+1)%3]|)
a=(2S-b+2)/2

接下来是代码部分:
技术图片
 1 #include <iostream>  
 2 #include<cmath>  
 3 #include<cstdlib>  
 4 #include<cstring>   
 5 #include<string>  
 6 #include <algorithm>  
 7 using namespace std;  
 8 typedef long long ll;  
 9   
10 struct point  
11 {  
12     int x,y;  
13 }p[5];  
14 int area()  
15 {  
16     int ans=0;  
17     for(int i=0 ; i<3 ; i++)  
18     {  
19        ans+=p[i].x*(p[(i+1)%3].y-p[(i+2)%3].y);  
20     }  
21      return ans;  
22 }  
23 int gcd(int a,int b)  
24 {  
25     return b==0?a:gcd(b,a%b);  
26 }  
27 int atline (point p1,point p2)  
28 {  
29     int b=fabs(p1.x-p2.x) , a=fabs(p1.y-p2.y);  
30     return gcd(a,b);  
31 }  
32 int main ()  
33 {  
34     bool flag;  
35     int i,n,s,lpoint;  
36      while (1)  
37     {     
38         flag=1;  
39         for ( i=0 ; i<3 ; i++)  
40             scanf("%d%d",&p[i].x,&p[i].y);  
41         for ( i=0 ; i<3 ; i++)  
42         if ( p[i].x || p[i].y )flag=0;  
43         if(flag) break;  
44         s=fabs(area())+2;  
45         for (i=0 ; i<3 ; i++)  
46         {  
47             lpoint+=atline(p[i],p[(i+1)%3]);  
48         }  
49         int ans=(s-lpoint)/2;  
50         printf("%d\n",ans);  
51     }  
52     return 0;  
53 }
View Code

 








皮克定理及其应用

标签:eve   break   main   scanf   open   应用   flag   problem   sed   

原文地址:https://www.cnblogs.com/guxingdetiankong/p/10714236.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!