题意:给N个点,求最多有多少个点在同一直线上
方法一:求出所有能形成的直线,两两比较,统计最多有多少条重合。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<set> 6 #include<iostream> 7 using namespace std; 8 typedef long long int64; 9 const int64 maxn = 701; 10 const int64 maxm = 490005; 11 const int64 inf = -123456789; 12 struct Point64{ 13 int x,y; 14 }pnt[ maxn ]; 15 struct Node{ 16 int64 a,b,c; 17 int x,y; 18 }tp[ maxm ]; 19 20 bool s[ maxn ]; 21 //set<int>s; 22 23 void init( int n ){ 24 for( int i=0;i<n;i++ ) 25 s[ i ] = false; 26 } 27 28 int64 cmp( Node p1,Node p2 ){ 29 if( p1.a!=p2.a ) return p1.a<p2.a; 30 else if( p1.b!=p2.b ) return p1.b<p2.b; 31 else return p1.c<p2.c; 32 } 33 34 int64 gcd( int64 a,int64 b ){ 35 int64 r; 36 while( b ){ 37 r = a%b; 38 a = b; 39 b = r; 40 } 41 return a; 42 } 43 44 void solve_gcd( int id ){ 45 int64 Gcd = gcd( tp[ id ].a,tp[ id ].b ); 46 Gcd = gcd( tp[ id ].c,Gcd ); 47 tp[ id ].a /= Gcd; 48 tp[ id ].b /= Gcd; 49 tp[ id ].c /= Gcd; 50 } 51 52 int main(){ 53 int n; 54 while( scanf("%d",&n)!=EOF ){ 55 for( int64 i=0;i<n;i++ ){ 56 cin>>pnt[i].x>>pnt[i].y; 57 //scanf("%lld%lld",&pnt[i].x,&pnt[i].y); 58 s[ i ] = false; 59 } 60 if( n==1||n==2 ){ 61 printf("%d\n",n); 62 continue; 63 } 64 int cnt = 0; 65 for( int i=0;i<n;i++ ){ 66 for( int j=i+1;j<n;j++ ){ 67 tp[ cnt ].a = pnt[ i ].y - pnt[ j ].y; 68 tp[ cnt ].b = pnt[ j ].x - pnt[ i ].x; 69 tp[ cnt ].c = pnt[ i ].x*pnt[ j ].y - pnt[ j ].x*pnt[ i ].y; 70 tp[ cnt ].x = i; 71 tp[ cnt ].y = j; 72 solve_gcd( cnt ); 73 cnt ++; 74 } 75 } 76 sort( tp,tp+cnt,cmp ); 77 int64 ans = 2; 78 int64 ans_tp = 0; 79 int64 prea = inf; 80 int64 preb = inf; 81 int64 prec = inf; 82 for( int i=0;i<cnt;i++ ){ 83 if(( prea!=tp[ i ].a || preb!=tp[ i ].b || prec!=tp[ i ].c )){ 84 ans_tp = 2; 85 prea = tp[ i ].a; 86 preb = tp[ i ].b; 87 prec = tp[ i ].c; 88 memset( s,false,sizeof( s ) ); 89 s[ tp[i].x ] = true; 90 s[ tp[i].y ] = true; 91 } 92 else { 93 if( s[ tp[i].x ]==false ) { 94 ans_tp ++; 95 s[ tp[i].x ] = true; 96 } 97 if( s[ tp[i].y ]==false ) { 98 ans_tp ++; 99 s[ tp[i].y ] = true; 100 } 101 ans = max( ans,ans_tp ); 102 } 103 } 104 cout<<ans<<endl; 105 //printf("%lld\n",ans); 106 } 107 return 0; 108 }