poj2606

题意:给定平面内的一些点,求平面内最多有几个点共线。

分析:枚举两个点确定一条线段,把所有线段按照斜率排序,然后整理斜率相等的即可获得答案。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
usingnamespace std;

#define eps 1.0e-8
#define maxn 205

struct XPoint
{
int x, y;
} point[maxn];

struct Line
{
int a, b;
double k;
bool end;
} line[maxn
* maxn];

int n, ncount;

booloperator<(const Line &a, const Line &b)
{
if (abs(a.k - b.k) > eps &&!a.end &&!b.end)
return a.k < b.k;
if (a.end != b.end)
return a.end < b.end;
if (a.a != b.a)
return a.a < b.a;
return a.b < b.b;
}

double getk(XPoint &a, XPoint &b)
{
return (b.y - a.y) *1.0/ (b.x - a.x);
}

int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d", &n);
for (int i =0; i < n; i++)
scanf(
"%d%d", &point[i].x, &point[i].y);
for (int i =0; i < n -1; i++)
for (int j = i +1; j < n; j++)
{
line[ncount].a
= i;
line[ncount].b
= j;
if (point[i].x == point[j].x)
line[ncount].end
=true;
else
{
line[ncount].end
=false;
line[ncount].k
= getk(point[i], point[j]);
}
ncount
++;
}
sort(line, line
+ ncount);
int start =0;
int ans =0;
for (int i =1; i < ncount; i++)
{
if (!((line[i].end && line[start].end) || ((!line[i].end &&!line[start].end) && (line[i].k - line[start].k)
< eps)) || line[i].a != line[start].a)
{
start
= i;
continue;
}
if (i - start > ans)
{
ans
= i - start;
}
}
printf(
"%d\n", ans +2);
return0;
}
posted @ 2011-05-15 13:12  金海峰  阅读(433)  评论(0编辑  收藏  举报