HDU 1705 Count the grid & jisuanke 35 三角形内点

2015-07-20 17:11:37 ? 作者: ? 浏览: 3

?

题意:

给出一个三角形,求三角形内的整点;
皮克定理:S=a/2+b-1; S为多边形面积;a为多边形边上的点; b为多边形内的点;
a为边上的点可以由欧几里得定理gcd(x1-x0,y1-y0)求得点数;

编程网站计蒜客35题也是一样的求法,只不过给出两点,实际写的话改成注释的那块就可以,链接:click here

代码:

?

#include 
  
   
#include 
   
     #include
     #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 using namespace std; #define lowbit(a) a&-a #define Max(a,b) a>b?a:b #define Min(a,b) a>b?b:a #define mem(a,b) memset(a,b,sizeof(a)) int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}}; const double eps = 1e-6; const double Pi = acos(-1.0); static const int inf= ~0U>>2; static const int N=30010; int scan() { int res = 0, flag = 0; char ch; if((ch = getchar()) == '-') flag = 1; else if(ch >= '0' && ch <= '9') res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + (ch - '0'); return flag ? -res : res; } void out(int a) { if(a < 0) { putchar('-'); a = -a; } if(a >= 10) out(a / 10); putchar(a % 10 + '0'); } int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } struct point { int x,y; } p[1000],pp[1000]; int acoss(point p1,point p2,point p0) { return abs((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x)); } int main() { int a,area,ans; while(cin>>p[0].x>>p[0].y>>p[1].x>>p[1].y>>p[2].x>>p[2].y) { //while(cin>>p[1].x>>p[1].y>>p[2].x) // { //p[0].x=0,p[0].y=0,p[2].y=0; if(!p[0].x&&!p[0].y&&!p[1].x&&!p[1].y&&!p[2].x&&!p[2].y)break; pp[0].x=abs(p[0].x-p[1].x); pp[0].y=abs(p[0].y-p[1].y); pp[1].x=abs(p[1].x-p[2].x); pp[1].y=abs(p[1].y-p[2].y); pp[2].x=abs(p[0].x-p[2].x); pp[2].y=abs(p[0].y-p[2].y); a=gcd(pp[0].x,pp[0].y)+gcd(pp[1].x,pp[1].y)+gcd(pp[2].x,pp[2].y); area=acoss(p[1],p[2],p[0]);//求S ans=(area-a+2)/2; printf(%d ,ans); } return 0; } 
               
              
             
            
           
          
         
        
       
      
     
   
  

When you want to give up, think of why you persist until now!


?

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: