2261: Triangle (皮克定理)

來源:互聯網
上載者:User
文章目錄
  • Input
  • Output
  • Sample Input
  • Sample Output
Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 277 51 Standard
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 x1 = y1 = 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 10 0 5 0 0 50 0 0 0 0 0
Sample Output
06/*
匹克定理(s=i+b/2-1)和輾轉相除的應用
*/
#include <cstdio>
#include <iostream>
#include <memory>
#include <cmath>
using namespace std;
struct point
{
    int x,y;
}p[5];
int area()
{
    int ans=0;
    for(int i=0 ; i<3 ; i++)
    {
        ans+=p[i].x*(p[(i+1)%3].y-p[(i+2)%3].y);
        //printf("%d ",ans);
    }
    return ans;
}
int gcd(int a,int b)
{
 return b==0?a:gcd(b,a%b);
}
int atline (point p1,point p2)
{
    int b=fabs(p1.x-p2.x) , a=fabs(p1.y-p2.y);
    return gcd(a,b);//返回邊上點的個數加上一個端點
}
int main ()
{
    int i,n;
    while (1)
    {
        bool flag=1;
        for ( i=0 ; i<3 ; i++)
        scanf("%d%d",&p[i].x,&p[i].y);
        for ( i=0 ; i<3 ; i++)
         if ( p[i].x || p[i].y )flag=0;
        if(flag) break;
        int s=fabs(area())+2,side=0;
        //cout<<s<<"/n";
        for (i=0 ; i<3 ; i++)
        {
            side+=atline(p[i],p[(i+1)%3]);
        }
        int ans=(s-side)/2;
        printf("%d/n",ans);
    }
    return 0;
}
/*
0 0 1 0 0 1
0 0 5 0 0 5
5 5 5 0 0 5
0 0 0 0 0 0
*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.