[关闭]
@geek-sjl 2018-10-18T04:18:07.000000Z 字数 1111 阅读 405

18图灵班作业5-编程

给定平面上两个格点P1和P2,求线段P1P2上除了P1和P2之外还有多少个格点。注:格点就是平面中横竖坐标值都为整数的点。直白说,就是给定两个格点,把它们连线,求连线上的其他格点的个数。

使使线

代码实现如下:

  1. #include <cstdio>
  2. #include <cmath>
  3. int binary_gcd(int a,int b){
  4. if(a==b) return a;
  5. if(a==0) return b;
  6. if(b==0) return a;
  7. if((~a&1)&&(~b&1)) return (binary_gcd(a>>1,b>>1))<<1;
  8. if((~a&1)&&(b&1)) return binary_gcd(a>>1,b);
  9. if((~b&1)&&(a&1)) return binary_gcd(a,b>>1);
  10. if(a>b) return binary_gcd((a-b)>>1,b);
  11. else return binary_gcd((b-a)>>1,a);
  12. }
  13. int main(){
  14. int x1,y1,x2,y2;
  15. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  16. printf("%d",binary_gcd(abs(x1-x2),abs(y1-y2))-1);
  17. return 0;
  18. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注