zoukankan      html  css  js  c++  java
  • POJ 2954 Pick定理

    题意:

    求三角形内的整点个数

    题解:

    pick定理。

    [Pick定理] 设以整数点为顶点的多边形的面积为S, 多边形内部的整数点数为N, 多边形边界上的整数点数为L, 则 N + L/2 - 1 = S

    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <cmath>
     7 
     8 using namespace std;
     9 //[Pick定理] 设以整数点为顶点的多边形的面积为S, 多边形内部的整数点数为N, 多边形边界上的整数点数为L, 则 N + L/2 - 1 = S
    10 struct PO
    11 {
    12     int x,y;
    13 }p[5];
    14 
    15 inline bool allzero()
    16 {
    17     for(int i=1;i<=3;i++)
    18         if(p[i].x!=0||p[i].y!=0) return false;
    19     return true;
    20 }
    21 
    22 inline int cross(PO &a,PO &b,PO &c)
    23 {
    24     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    25 }
    26 
    27 inline int gcd(int a,int b)
    28 {
    29     int ys;
    30     while(b)
    31     {
    32         ys=a%b;
    33         a=b; b=ys;
    34     }
    35     return a;
    36 }
    37 
    38 inline int getnum(PO &a,PO &b)
    39 {
    40     int x=abs(a.x-b.x),y=abs(a.y-b.y);
    41     return gcd(x,y)-1;
    42 }
    43 
    44 inline void go()
    45 {
    46     int area=abs(cross(p[1],p[2],p[3]));
    47     int edgenum=getnum(p[1],p[2])+getnum(p[1],p[3])+getnum(p[2],p[3])+3;
    48     printf("%d\n",(area-edgenum)/2+1);
    49 }
    50 
    51 int main()
    52 {
    53     while(scanf("%d%d%d%d%d%d",&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y))
    54     {
    55         if(allzero()) break;
    56         go();
    57     }
    58     return 0;
    59 } 
  • 相关阅读:
    人生本来就是一种修行
    Go的一些趣味题库
    PHP系统常被挂马的代码
    PHP加密字符串函数(解密)
    photoshop
    截图
    用手机作为摄像头
    IM 学习记录
    编译 学习过程
    过程流水记录-编译Lua srlua使用iup-完结
  • 原文地址:https://www.cnblogs.com/proverbs/p/2924640.html
Copyright © 2011-2022 走看看