POJ 1118

 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
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值