Triangle

Triangle

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 14
Problem 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
PKU
 1 #include <string.h>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 int x1,y1,x2,y2,x3,y3;
 5 int ABS(int x)
 6 {
 7     return x>0?x:-x;
 8 }
 9 int GCD(int a,int b)
10 {
11     if(b==0) return a;
12     return GCD(b,a%b);
13 }
14 
15 int Find_OnPoint() 
16 {
17     int sum=0;
18     sum+=GCD(ABS(x1-x2),ABS(y1-y2));
19     sum+=GCD(ABS(x2-x3),ABS(y2-y3));
20     sum+=GCD(ABS(x1-x3),ABS(y1-y3));
21     return sum;
22 }
23 int P1_X_P2()
24 {
25     return ABS((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1));
26 }
27 int main()
28 {
29     int S;
30     int sum;
31     while(scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF)
32     {
33         sum=0;
34         if(x1==0&&y1==0&&x2==0&&y2==0&&x3==0&&y3==0)break;
35         S=P1_X_P2()/2;
36         sum+=Find_OnPoint();
37         printf("%d\n",(S+1-sum/2));
38     }
39     return 0;
40 }
View Code

 

posted @ 2014-08-22 13:51  Wurq  阅读(179)  评论(0编辑  收藏  举报