您的位置:首页 > 其它

UVa 270 & POJ 1118 - Lining Up

2013-07-22 12:33 357 查看
  题目大意:给一些点,找出一条直线使尽可能多的点在这条直线上,求这条直线上点的个数。

  以每一个点为原点进行枚举,求其它点的斜率,斜率相同则说明在一条直线上。对斜率排序,找出斜率连续相等的最大长度。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAXN 700+10
#define PRECISION 10e-9

struct Point
{
int x, y;
};

Point point[MAXN];
double slope[MAXN];

int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int N;
scanf("%d", &N);
char s[100];
getchar();
gets(s);
while (N--)
{
int n = 0; // the number of points
while (gets(s))
{
if (s[0] == '\0')   break;
sscanf(s, "%d%d", &point
.x, &point
.y);
n++;
}
int ans = 0;
for (int i = 0; i < n; i++)
{
int zero_cnt = 0, k = 0;
for (int j = 0; j < n; j++)
if (i != j)
{
if (point[i].x == point[j].x)   zero_cnt++;
else
{
slope[k] = 1.0 * (point[j].y - point[i].y) / (point[j].x - point[i].x);
k++;
}
}
sort(slope, slope+k);
int same_cnt = 1, maxl = 1;
for (int i = 1; i <= k; i++)
{
if (i < k && fabs(slope[i] - slope[i-1]) < PRECISION)
{
same_cnt++;
}
else
{
maxl = max(maxl, same_cnt);
same_cnt = 1;
}
}
maxl = max(maxl, zero_cnt);
ans = max(ans, maxl+1);
}
printf("%d\n", ans);
if (N) printf("\n");
}
return 0;
}


View Code
  开始WA了两次,各种纠结,也找不出什么问题,忽然想到会不会是精度设的太大了,从10e-6改成10e-9,然后抱着侥幸心理提交了,竟然AC了...这个...算是积累经验吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: