http://poj.org/problem?id=1118
求最多有多少个点共线
枚举一个点,看与这个点斜率相同的点最多有多少个,再取最大值
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<algorithm>
#define M 1000
#define eps 1e-8
#define INF 0xfffffff
using namespace std;
struct point
{
int x,y;
}po[M];
int n;
double xl[M*M];
double solve(point a,point b)
{
if(a.x!=b.x)
{return (a.y-b.y)*1.0/(a.x-b.x);}
else {return INF;}
}
int main()
{
int i,j,k;
int num,num1;
int maxn;
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;i++)
scanf("%d%d",&po[i].x,&po[i].y);
maxn=-1;
for(i=0;i<n;i++)
{
k=0;num1=0;
for(j=i+1;j<n;j++)
{
double tmp=solve(po[i],po[j]);
if(fabs(tmp-INF*1.0)<eps)
num1++;//斜率不存在的情况
else xl[k++]=tmp;
}
sort(xl,xl+k);
num=1;
for(j=1;j<k;j++)
{
if(xl[j]-xl[j-1]<eps)
num++;
else num=1;
if(num>maxn)
maxn=num;
}
if( num1>maxn)
maxn=num1;
}
printf("%d\n",maxn+1);
}
return 0;
}
/*
3
1 2
2 4
1 3
*/