题目:给你平面上n个点,求最多有几个点共线。
分析:计算几何。枚举基准点,其他点按基准点极角排序(斜率排序),然后枚举相邻直线判断统计即可。
说明:时间复杂度O(n*n*lg(n))。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef struct pnode
{
double x,y;
}point;
point P[701];
double S[701];
int main()
{
int t;
char buf[100];
scanf("%d",&t);
getchar();
gets(buf);
while (t --) {
int count = 0;
while (gets(buf) && buf[0]) {
sscanf(buf, "%lf%lf", &P[count].x,&P[count].y);
count ++;
}
int max = 0;
for (int i = 0 ; i < count ; ++ i) {
int save = 0;
for (int j = 0 ; j < count ; ++ j) {
if (j == i) continue;
if (P[i].x == P[j].x)
S[save ++] = 1e1000;
else
S[save ++] = (P[j].y-P[i].y)/(P[j].x-P[i].x);
}
sort(S, S+save);
int now = 1;
for (int j = 1 ; j < save ; ++ j)
if (S[j] == S[j-1])
now ++;
else {
if (max < now) max = now;
now = 1;
}
if (max < now) max = now;
}
printf("%d\n",max+1);
if (t) printf("\n");
}
return 0;
}
原文:http://blog.csdn.net/mobius_strip/article/details/44159451