POJ 2954 Triangle pick公式

来源:互联网 发布:手机版仓库进销存软件 编辑:程序博客网 时间:2024/06/02 00:04

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;}
原创粉丝点击