有多少个整点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yky__xukai/article/details/81453457

                                                     有多少个整点                                                         

Description

 给定平面坐标系中的两点A(x1,y1)和B(x2,y2),通过这两点做一条线段,求这条线段在坐标系中经过的整数点的个数(包括A和B本身这两点)。

Input

输入的第一行一个整数T,表示测试样例数。 
接下来T行,每行4个整数用空格隔开,分别表示x1,y1, x2,y2.(0<=xi,yi<=2^32-1)

Ouput

输出通过A和B两点做一条线段,该线段在坐标系中经过的整数点的个数(包括A和B本身这两点)。

Sample Input

5

0 0 3 3

1 1 2 2

0 0 2 5

123369 963 258 741

12300000 2000 2123000 999990

Sample Output

4

2

2

4

11

Hint

此题要用两坐标整数个数的最大公约数做,否则超时。

欧几里得算法求最大公约数链接

#include <iostream>
#include <cmath>
using namespace std;
int abs(int a,int b){
	return (a-b)>0?(a-b):(b-a);
}
int gcd(int c,int d){
	if(c<d)
	{
		int r = c;
		c = d;
		d = r;	
	}
	return !d?c:gcd(d,c%d);//d==0 return c else retrun ...
}
int main()
{
	int T,x1,y1,x2,y2;
	cin>>T;
	for(int i=0;i<T;i++){
		cin>>x1>>y1>>x2>>y2; 
		cout<<gcd(abs(x1-x2),abs(y1-y2))+1<<endl;
	}
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/yky__xukai/article/details/81453457