POJ 2954 Triangle (Pick定理)
给定三角形,求出给定三角形内部整点的个数。
1 /* 2 Memory: 116 KB Time: 0 MS 3 Language: C++ Result: Accepted 4 By coolwind 5 */ 6 #include <stdio.h> 7 #include <string.h> 8 9 #define MAXN 105 10 struct Point { 11 int x,y; 12 Point(int a = 0, int b = 0) 13 :x(a),y(b){} 14 }; 15 16 Point pt[3]; 17 18 int gcd(int a, int b) 19 { 20 if(b == 0) return a; 21 return gcd(b,a%b); 22 } 23 24 int abs(int x) 25 { 26 if( x < 0) return -x; 27 return x; 28 } 29 30 int Xmul(Point a ,Point b, Point o) 31 { 32 return (a.x - o.x) * (b.y - o.y) - 33 (a.y - o.y) * (b.x - o.x); 34 } 35 36 int main() 37 { 38 while(true) 39 { 40 scanf("%d%d%d%d%d%d",&pt[0].x,&pt[0].y, 41 &pt[1].x,&pt[1].y,&pt[2].x,&pt[2].y); 42 if(pt[0].x == 0&&pt[0].y == 0&&pt[1].x == 0 43 &&pt[1].y == 0&&pt[2].x == 0&&pt[2].y == 0) 44 break; 45 int area = Xmul(pt[1],pt[2],pt[0]); 46 int on = 0; 47 for(int i = 0 ;i < 3; i ++) 48 on += gcd(abs(pt[(i + 1) % 3].x - pt[i].x) 49 ,abs(pt[(i + 1) % 3].y - pt[i].y)); 50 int in = (abs(area) - on + 2) / 2; 51 printf("%d\n",in); 52 } 53 return 0; 54 }

浙公网安备 33010602011771号