ACM乐园
Love Me,Love My Code!
posts - 53,  comments - 24,  trackbacks - 0
http://acm.hdu.edu.cn/showproblem.php?pid=1577

判断三点共线公式为:(x2-x1)*(y3-y1)=(y2-y1)*(x3-x1)。刚开始我采用该公式判断,发现超时了,网上看了下发现了求公约数的方法,这种方法用到了相似三角形原理。

对于A、C两点它们之间如果存在整数点F与它们共线,则必有AB/BC=AE/EF,则AB与BC必有不为1的公约数,所以现在的问题由判断是否存在整数点共线变成横向距离和纵向距离是否互质了。这就很好判断了,以下是AC的代码。


 1#include <iostream>
 2#include <algorithm>
 3#include <string>
 4#include <set>
 5#include <cmath>
 6#include <iomanip>
 7using namespace std;
 8
 9int gcd(int a,int b)
10{
11    int temp;
12    while(b)
13    {
14        temp=b;
15        b=a%b;
16        a=temp;
17    }

18    return a;
19}

20
21int main()
22{
23    int l,x1,y1,x2,y2;
24    while(cin>>l,l)
25    {
26        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
27        if(abs(x2)>|| abs(y2)>l)
28        {
29            cout<<"Out Of Range\n";
30            continue;
31        }

32        if(gcd(abs(x1-x2),abs(y1-y2))==1)
33            cout<<"Yes\n";
34        else
35            cout<<"No\n";
36    }

37    return 0;
38}
posted on 2011-04-01 22:11 大大木马 阅读(347) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2011年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

常用链接

留言簿(1)

随笔档案(53)

文章档案(2)

搜索

  •  

积分与排名

  • 积分 - 62912
  • 排名 - 351

最新评论

阅读排行榜

评论排行榜