HDU 1577 WisKey的眼神

WisKey的眼神

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 130 Accepted Submission(s): 61
 
Problem Description
WisKey的眼镜有500多度,所以眼神不大好,而且他有个习惯,就是走路喜欢看着地(不是为了拣钱哦^_^),所以大家下次碰见他的时候最好主动打下招呼,呵呵.
但是Rabbit总是喜欢扮神秘,一天WisKey去食堂排队等着买饭,突然收到一道短消息,是Rabbit发的,”呵呵,又看见你了,你没看到我吧”.WisKey马上拉长脖子扫描食堂,可是就是看不到,再发短信问Rabbit在哪,Rabbit回信曰”我已经在寝室了”.WisKey无语....
假设食堂是个正方形,食堂中心坐标为(0,0),长度为2*L, WisKey保证在食堂内.
因为是吃饭高峰期,所以每个点上都站着人,当某些人处在同一直线上时就有可能被前面的人挡住.
聪明的ACMer请你帮帮WisKey,告诉他能不能看见Rabbit.
 
Input
输入L,sx,sy,px,py; L<=1000,sx,sy是WisKey的坐标,px,py是Rabbit的坐标.
以L=0为结束.
 
Output
对于每组输入数据,能看见输出”Yes”,看不见输出”No”.
Rabbit不在食堂输出”Out Of Range”.
 
Sample Input
5 0 0 1 1
5 0 0 2 0
5 0 0 6 6
5 0 0 -1 -1
0
 
Sample Output
Yes
No
Out Of Range
Yes
 

题目意思就是给个矩阵 , 然后判下两个点连成一条直线 , 中间还有没其他整数点 。

排除那些 X轴,Y轴 相等 的情况 然后横着坐标做差求个GCD判一下是否等于1就行了。 

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

typedef pair<int,int> Point;
#define x first
#define y second

int gcd( int a , int b ){ return b==0 ? a : gcd( b , a % b ) ; }

int main()
{
    ios::sync_with_stdio(0);
    int L ;
    Point s ,e ;
    while( cin >> L && L ){
        cin>>s.x>>s.y>>e.x>>e.y;
        if( e.x > L || e.x < -L || e.y >L || e.y <-L ){ cout<<"Out Of Range" <<endl; continue ;}
        if( ( s.x == e.x && abs(s.y - e.y ) > 1 ) || ( s.y == e.y && abs( s.x - e.x ) > 1 )  ){ cout<< "No" <<endl ;continue ;}
        if( gcd( abs( s.x - e.x ) , abs( s.y - e.y )) != 1  ) cout<< "No" <<endl ;
        else  cout << "Yes" <<endl;

    }
}
only strive for your goal , can you make your dream come true ?
原文地址:https://www.cnblogs.com/hlmark/p/3990564.html