POJ 2954 Triangle pick公式

Pick公式:平面上以格子点为顶点的简单多边形,如果边上的点数为on,内部的点数为in,则它的面积为area=on/2+in-1

利用gcd求每个边上的点数。

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;

struct point
{
    int x, y;
}p[5];

int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a%b);
}

int main()
{
    int i, j;
    while( ~scanf("%d%d", &p[0].x, &p[0].y) )
    {
        bool flag = 1;
        for(i = 1; i < 3; i++)
        {
            scanf("%d%d", &p[i].x, &p[i].y);
            if(p[i].x || p[i].y) flag = 0;
        }
        if(flag && !p[0].x && !p[0].y) break;
        int on = 0;
        p[3] = p[0];
        for(i = 0; i < 3; i++)
            on += gcd( abs (p[i].x - p[i+1].x), abs(p[i].y - p[i+1].y)  );
        int s = (p[1].x - p[0].x) * (p[2].y - p[0].y) - (p[1].y - p[0].y) * (p[2].x - p[0].x);
        s = abs(s) / 2;
        int in = s + 1 - on / 2;
        printf("%d\n", in);
    }
    return 0;
}
posted @ 2012-09-03 17:50  To be an ACMan  Views(153)  Comments(0)    收藏  举报