Pick公式:平面上以格子点为顶点的简单多边形的面积=边上的点数/2+内部的点数+1。
代码如下:
----------------------------------------------------------------------------------------------------------------------------
#include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int MAXN = 17; const double EPS = 1e-10; struct point { int x, y; }p[MAXN]; int GCD(int m, int n) { if(!m || !n) return m+n; return GCD(n, m%n); } int main() { while(1) { int ok=0; for(int i=0; i<3; i++) { scanf("%d%d", &p[i].x, &p[i].y); if(p[i].x || p[i].y) ok = 1; } if(!ok)break; p[3] = p[0]; int cnt=0, area=0; for(int i=0; i<3; i++) { cnt += GCD(abs(p[i].x-p[i+1].x), abs(p[i].y-p[i+1].y)); area += p[i].x*p[i+1].y - p[i].y*p[i+1].x; } if(area < 0)area = -area; printf("%d\n", (area-cnt)/2+1); } return 0; }
时间: 2024-10-13 07:41:15