zoukankan      html  css  js  c++  java
  • poj 2954 Triangle(Pick定理)

    Triangle
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5546   Accepted: 2410

    Description

    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 x1y1 = 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

    Source

    【思路】

           Pick定理

           定理如下:

                  S=a+b/2-1

           其中S代表多边形面积,a代表多边形内部的整点数,b代表多边形边上的整点数。

           其中b=gcd(|x2-x1|,|y2-y1|)

    【代码】

     1 #include<cstdio>
     2 using namespace std;
     3 
     4 int gcd(int a,int b) {
     5     return b==0? a:gcd(b,a%b);
     6 }
     7 int x1,y1,x2,y2,x3,y3;
     8 int abs(int x) { return x<0? -x:x; }
     9 int S() { 
    10     return abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2;
    11 }
    12 int calc(int x1,int y1,int x2,int y2) {
    13     return gcd(abs(x2-x1),abs(y2-y1));
    14 }
    15 int main() {
    16     while(scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3)==6) {
    17         if(!x1 && !y1 && !x2 && !y2 && !x3 && !y3) break;
    18         int b=calc(x1,y1,x2,y2)+calc(x2,y2,x3,y3)+calc(x3,y3,x1,y1);
    19         printf("%d
    ",S()-b/2+1);
    20     }
    21     return 0;
    22 }
  • 相关阅读:
    header头参数 确定该文件类型
    phpexcel 使用说明
    杂七杂八 各种小知识
    php 后知后觉
    限制SSH登录失败次数
    DES和AES密码之间的区别 & 对称加密算法DES、3DES和AES 原理总结
    加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用
    加密算法学习总结---DES-CBC加密算法 & 分组加密的四种模式
    Linux下进程间通信方式——共享内存
    fork()+pipe() --&gt; 父子进程间通过管道通信 Linux系统编程pipe()
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5182285.html
Copyright © 2011-2022 走看看