还是pick定理,直接套用模板,具体请参考上两篇。
给你一个三角形,求出三角形内部的整数点的个数。
#include <iostream> #include <cstdio> #include <cstdlib> //#include <cstring> #include <cmath> using namespace std; struct POINT { int x,y; }point[110]; int n; double getArea() { double sum = 0; for (int i = 0; i < n; ++ i) { sum += (point[i].x * point[(i + 1) % n].y - point[i].y * point[(i + 1) % n].x ); } return fabs(sum/2.0); } int Gcd(int a, int b) { if (0 == b) { return a; } else return Gcd(b, a % b); } int getSegmentPoint(POINT p1, POINT p2) { int a = abs(p2.y - p1.y); int b = abs(p2.x - p1.x); if (a == 0 && b == 0) { return 0; } if (a == 0) { return b - 1; } if (b == 0) { return a - 1; } return Gcd(b, a) - 1; } int getPoint() { int ans = n; for (int i = 0; i < n; ++ i) { ans += getSegmentPoint(point[i], point[(i + 1) % n]); } return ans; } int main() { //int cas, j = 1; //scanf("%d", &cas); n = 3; while (1) { scanf("%d %d %d %d %d %d", &point[0].x, &point[0].y, &point[1].x, &point[1].y, &point[2].x, &point[2].y); //point[0].x = point[0].y = 0; //for (int i = 1; i <= n; ++ i) //{ // scanf("%d %d", &point[i].x, &point[i].y); // point[i].x += point[i - 1].x; // point[i].y += point[i - 1].y; // //cin >> point[i].x >> point[i].y; //} //printf("Scenario #%d:\n", j ++); if (point[0].x == 0 && point[0].y == 0 && point[1].x == 0 && point[1].y == 0 && point[2].x == 0 && point[2].y == 0) { break; } double Area = getArea(); int res; int PointNum = getPoint(); res = (int)Area - PointNum / 2 + 1; printf("%d\n", res); } return 0; }