标签:
思路:首先把所有点按照坐标排序,然后以每一个点为基准点,计算其他点与这个点连线的斜率,将所有斜率排序后求最多的相同的斜率数即可。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <stack> 12 using namespace std; 13 typedef long long LL; 14 const LL INF = 0x4fffffff; 15 const double EXP= 1e-6; 16 const LL MOD = 1e9+7; 17 const int MS= 1e5 + 5 ; 18 19 20 struct point 21 { 22 int x; 23 int y; 24 }points[MS]; 25 26 double k[MS]; 27 28 int cmp(const point &a,const point &b) 29 { 30 if(a.x != b.x) 31 return a.x < b.x; 32 return a.y < b.y; 33 } 34 35 double slope(point a,point b) 36 { 37 if(a.x == b.x) 38 return INF; 39 double px = a.x - b.x; 40 double py = a.y - b.y; 41 return py / px; 42 } 43 44 int main() 45 { 46 int n; 47 int i,j,cnt,t,maxv; 48 while(scanf("%d",&n) != EOF && n) 49 { 50 for(i = 0;i < n;i++) 51 scanf("%d%d",&points[i].x,&points[i].y); 52 sort(points,points+n,cmp); 53 maxv = -1; 54 for(i=0;i < n;i++) 55 { 56 t=0; 57 for(j = i+1;j < n;j++) 58 { 59 k[t++] = slope(points[i],points[j]); 60 } 61 sort(k,k+t); 62 for(j = 1,cnt = 1;j < t;j++) 63 { 64 if(fabs(k[j]-k[j-1]) < EXP) 65 cnt++; 66 else 67 cnt=1; 68 if(cnt > maxv) 69 maxv = cnt; 70 } 71 } 72 printf("%d\n",maxv+1); 73 } 74 return 0; 75 }
Lining Up Rabbit hunt poj 1118 poj 2606 点共线问题。
标签:
原文地址:http://www.cnblogs.com/hutaishi/p/4695863.html