poj2606

本文介绍了一种解决平面内多个点中最多有多少个点共线的问题的方法。通过枚举两个点来确定线段,对所有线段按斜率排序,并统计斜率相同的线段数量以得出最终答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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;
}

转载于:https://www.cnblogs.com/rainydays/archive/2011/05/15/2046847.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值