题意:
给你一堆二维点,求最大共线点数
思路:
暴力,先枚举前两个点,第三个点则采用三点共线公式:
(x2-x1)/(y2-y1)=(x3-x2)/(y3-y2)
->(x2-x1)(y3-y2) = (x3-x2)(y2-y1)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 705;
int x[N], y[N], n;
char str[N];
int find(int a, int b, int c) {
int cnt = 0;
for (int k = c + 1; k < n; k++) {//k--3,c--2
if (a*(y[k] - y[c]) == (x[k] - x[c])*b)
cnt++;
}
return cnt;
}
int main() {
int cas;
scanf("%d\n", &cas);
while (cas--) {
n = 0;
while (gets(str) ) {
if(!str[0] &&n)break;
sscanf(str, "%d %d", &x[n], &y[n]);
n++;
}
int maxn = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int cur = find(x[j] - x[i], y[j] - y[i], j) + 2;
maxn = max(maxn, cur);
}
}
printf("%d\n", maxn);
if (cas)
printf("\n");
}
return 0;
}