uva270 - Lining Up(枚举)
给出一些点,看最多有多少个点在同一个直线上。
用每一个点作为起点,枚举与其他点的斜率,找出最大值。注意输出格式 和初始化最小ans为2;
题目:
Lining Up |
``How am I ever going to solve this problem?" said the pilot.
Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number?
Your program has to be efficient!
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input consists of N pairs of integers,
where 1 < N < 700. Each pair of integers is separated by one blank
and ended by a new-line character. The list of pairs is ended with an
end-of-file character. No pair will occur twice.
Output
For each test case, the output must follow the description below. The outputs
of two consecutive cases will be separated by a blank line.
The output
consists of one integer representing the largest number of points that all lie
on one line.
Sample Input
1 1 1 2 2 3 3 9 10 10 11
Sample Output
3
代码:
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 using namespace std; 8 double K[1000000]; 9 double X[1000]; 10 double Y[1000]; 11 int main() 12 { 13 freopen("in.txt","r",stdin); 14 int n; 15 char tmp[10]; 16 cin>>n; 17 cin.get(); 18 cin.get(); 19 while(n--) 20 { 21 int p=0; 22 // getchar(); 23 while(gets(tmp)) 24 { 25 //cout<<"tmp"<<tmp<<endl; 26 if(!tmp[0])break; 27 sscanf(tmp,"%lf %lf",&X[p],&Y[p]); 28 // cout<<"X Y "<<X[p]<<" "<<Y[p]<<endl; 29 p++; 30 } 31 int pk=0,ans=2; 32 for(int i=0;i<p;i++) 33 { 34 pk=0; 35 for(int j=0;j<p;j++) 36 { 37 if(i==j)continue; 38 //cout<<Y[j]<<" "<<Y[i]<<" "<<X[j]<<" "<<X[i]<<endl; 39 K[pk++] = (Y[j]-Y[i])/(X[j]-X[i]); 40 //cout<<K[pk-1]<<endl; 41 } 42 sort(K,K+pk); 43 //cout<<"ok"<<endl; 44 int tans=2; 45 for(int i=0;i<pk-1;i++) 46 { 47 int j=i,m=j+1; 48 tans=2; 49 while(K[j]==K[m]) 50 { 51 tans++;j++;m++; 52 } 53 if(tans>ans)ans=tans; 54 } 55 } 56 cout<<ans<<endl; 57 if(n)cout<<endl; 58 } 59 return 0; 60 }