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 }
  • 相关阅读:
    [python] defaultdict
    [VM workstation]VM workstation 中的虚拟机连不上网络
    [Docker] docker 基础学习笔记1(共6篇)
    [python] import curses
    servlet 产生随机验证码
    JSTL标签库 sql标签
    JSTLLearning
    Ajax实现联想(建议)功能
    HDU 1241
    Servlet实现文件上传
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5182285.html
Copyright © 2011-2022 走看看