2261: Triangle (皮克定理)

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 277 51 Standard
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
/*
匹克定理(s=i+b/2-1)和辗转相除的应用
*/
#include <cstdio>
#include <iostream>
#include <memory>
#include <cmath>
using namespace std;
struct point
{
    int x,y;
}p[5];
int area()
{
    int ans=0;
    for(int i=0 ; i<3 ; i++)
    {
        ans+=p[i].x*(p[(i+1)%3].y-p[(i+2)%3].y);
        //printf("%d ",ans);
    }
    return ans;
}
int gcd(int a,int b)
{
 return b==0?a:gcd(b,a%b);
}
int atline (point p1,point p2)
{
    int b=fabs(p1.x-p2.x) , a=fabs(p1.y-p2.y);
    return gcd(a,b);//返回边上点的个数加上一个端点
}
int main ()
{
    int i,n;
    while (1)
    {
        bool flag=1;
        for ( i=0 ; i<3 ; i++)
        scanf("%d%d",&p[i].x,&p[i].y);
        for ( i=0 ; i<3 ; i++)
         if ( p[i].x || p[i].y )flag=0;
        if(flag) break;
        int s=fabs(area())+2,side=0;
        //cout<<s<<"/n";
        for (i=0 ; i<3 ; i++)
        {
            side+=atline(p[i],p[(i+1)%3]);
        }
        int ans=(s-side)/2;
        printf("%d/n",ans);
    }
    return 0;
}
/*
0 0 1 0 0 1
0 0 5 0 0 5
5 5 5 0 0 5
0 0 0 0 0 0
*/

你可能感兴趣的:(2261: Triangle (皮克定理))