UVa 270 & POJ 1118 - Lining Up
题目大意:给一些点,找出一条直线使尽可能多的点在这条直线上,求这条直线上点的个数。
以每一个点为原点进行枚举,求其它点的斜率,斜率相同则说明在一条直线上。对斜率排序,找出斜率连续相等的最大长度。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 #define MAXN 700+10 6 #define PRECISION 10e-9 7 8 struct Point 9 { 10 int x, y; 11 }; 12 13 Point point[MAXN]; 14 double slope[MAXN]; 15 16 int main() 17 { 18 #ifdef LOCAL 19 freopen("in", "r", stdin); 20 #endif 21 int N; 22 scanf("%d", &N); 23 char s[100]; 24 getchar(); 25 gets(s); 26 while (N--) 27 { 28 int n = 0; // the number of points 29 while (gets(s)) 30 { 31 if (s[0] == '\0') break; 32 sscanf(s, "%d%d", &point[n].x, &point[n].y); 33 n++; 34 } 35 int ans = 0; 36 for (int i = 0; i < n; i++) 37 { 38 int zero_cnt = 0, k = 0; 39 for (int j = 0; j < n; j++) 40 if (i != j) 41 { 42 if (point[i].x == point[j].x) zero_cnt++; 43 else 44 { 45 slope[k] = 1.0 * (point[j].y - point[i].y) / (point[j].x - point[i].x); 46 k++; 47 } 48 } 49 sort(slope, slope+k); 50 int same_cnt = 1, maxl = 1; 51 for (int i = 1; i <= k; i++) 52 { 53 if (i < k && fabs(slope[i] - slope[i-1]) < PRECISION) 54 { 55 same_cnt++; 56 } 57 else 58 { 59 maxl = max(maxl, same_cnt); 60 same_cnt = 1; 61 } 62 } 63 maxl = max(maxl, zero_cnt); 64 ans = max(ans, maxl+1); 65 } 66 printf("%d\n", ans); 67 if (N) printf("\n"); 68 } 69 return 0; 70 }
开始WA了两次,各种纠结,也找不出什么问题,忽然想到会不会是精度设的太大了,从10e-6改成10e-9,然后抱着侥幸心理提交了,竟然AC了...这个...算是积累经验吧