题目要求是说给你一系列点,要求你求出一条直线上最多包含的点数
思路:对坐标进行按x从小到大,y从小到大作为排序,然后枚举每个点作为起点,算剩余点与该点的斜率,最后统计斜率相同的最多的个数即可。
需要注意的是如果点数为2则答案应该是2(WA了几次)
还有一点是当斜率不存在的时候需要特殊赋值为最大值
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1001;
const int inf=1<<28;
struct node
{
int x;
int y;
bool operator < (const node &a)const
{
if(x==a.x)
return y<a.y;
return x<a.x;
}
}a[maxn];
int n,ans;
double pos[maxn];
int main()
{
while(scanf("%d",&n)&&n)
{
ans=1;
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a,a+n);
for(int i=0;i<n;i++)
{
int cur=0;
for(int j=i+1;j<n;j++)
{
if(a[j].x-a[i].x==0)
pos[cur++]=inf;
else
pos[cur++]=(a[j].y-a[i].y)*1.0/(a[j].x-a[i].x);
}
sort(pos,pos+cur);
int cnt=1;
for(int j=1;j<cur;j++)
{
if(fabs(pos[j]-pos[j-1])<1e-6)
{
cnt++;
ans=max(ans,cnt);
}
else
cnt=1;
}
}
printf("%d\n",ans+1);
}
return 0;
}