一开看了下数据1000,比较小,因为要枚举靠经直线的点,由于闲麻烦于是直接枚举两个点构成的直线所在正方形的所有点,结果超时。感觉数据量并不大。
后来只能枚举靠经直线的点,但是想了想枚举靠近直线的点感觉非常难做到,于是思路反转一下,其实可以没觉对应两点过横坐标之间的值作为x,然后通过直线公式计算出y,现在只在判断y是否是整数就好了,因为如果是整数说明存在这样的整数点在这条直线上。结果就是:如果y不是整数输出Yes 否者输出No. 这个方法本来很容易实现,结果这题卡我的精度,在判断y是否是整数的那个卡了我的精度,坑死了。后来网上看了下原来还有更简单的办法,就是判断dy 和 dx是否存在公约数,想想觉得这个方法的确巧妙。
枚举点法:
/* * this code is made by LinMeiChen * Problem: * Type of Problem: * Thinking: * Feeling: */ #include<iostream> #include<algorithm> #include<stdlib.h> #include<string.h> #include<stdio.h> #include<math.h> #include<string> #include<vector> #include<queue> #include<list> using namespace std; typedef long long lld; typedef unsigned int ud; #define oo 0x3f3f3f3f #define ep 1e-9 int main() { double sx, sy, px, py, L, temp; int x, xx; bool can; while (scanf("%lf", &L) && L!=0.0) { scanf("%lf %lf %lf %lf", &sx, &sy, &px, &py); if (fabs(px) > L || fabs(py) > L) { puts("Out Of Range"); continue; } if (px == sx) { if (fabs(py - sy) <= 1.0) puts("Yes"); else puts("No"); continue; } if (py == sy) { if (fabs(px - sx) <= 1.0) puts("Yes"); else puts("No"); continue; } can = true; x = min(sx, px); xx = max(sx, px); for (int i = x + 1; i < xx; i++) { temp = (1.0*i - sx)*(py - sy) / (px - sx) + sy; if (temp == (double)(int)temp) { can = false; break; } } if (can) puts("Yes"); else puts("No"); } return 0; } /* 题目数据: 5 0 0 1 1 5 0 0 2 0 5 0 0 6 6 5 0 0 -1 -1 第一种情况: 5 1 0 2 0 5 1 0 3 0 5 0 1 0 2 5 0 1 0 3 第二种情况: 5 5 5 -5 -5 5 -1 -1 1 1 5 0 0 4 2 10 3 1 6 2 */公倍数法:
/* * this code is made by LinMeiChen * Problem: * Type of Problem: * Thinking: * Feeling: */ #include<iostream> #include<algorithm> #include<stdlib.h> #include<string.h> #include<stdio.h> #include<math.h> #include<string> #include<vector> #include<queue> #include<list> using namespace std; typedef long long lld; typedef unsigned int ud; #define oo 0x3f3f3f3f #define ep 0.0005 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } int main() { double sx, sy, px, py, L, temp; int x, xx; bool can; while (scanf("%lf", &L) && L != 0.0) { scanf("%lf %lf %lf %lf", &sx, &sy, &px, &py); if (fabs(px) > L || fabs(py) > L) { puts("Out Of Range"); continue; } if (px == sx) { if (fabs(py - sy) <= 1.0) puts("Yes"); else puts("No"); continue; } if (py == sy) { if (fabs(px - sx) <= 1.0) puts("Yes"); else puts("No"); continue; } int ans = gcd((int)abs(px - sx), (int)abs(py - sy)); if (ans > 1) puts("No"); else puts("Yes"); } return 0; }