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 }
  • 相关阅读:
    .net类库里ListView的一个BUG
    获取lable的caption, 摘抄还未测试可用否
    (转) lua实现split的简易方法
    2. SharePoint Online 开发,请联系qq512800530。加好备注。(不要发站内信。。。)
    1. android
    开发人员应关注的20个jQuery网站/博客
    Temp
    彩票项目开发节项
    求android ble 解决方案!
    自己开发的工作流引擎
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5182285.html
Copyright © 2011-2022 走看看