POJ - 1118 Lining Up

题意:求一条线上最多几个点

思路:枚举一个点,然后求出过这个点的直线的斜率来求最大值

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 710;
const int INF = 1e7;

int arr[MAXN][2],n;
float brr[MAXN];

int main() {
	while (scanf("%d", &n) != EOF && n) {
		for (int i = 0; i < n; i++)
			scanf("%d%d", &arr[i][0], &arr[i][1]);
		int Max = 2;
		int cnt,num;
		for (int i = 0; i < n; i++) {
			cnt = 0;
			num = 2;
			for (int j = i+1; j < n; j++) {
				if (arr[i][0] == arr[j][0])
					brr[cnt++] = INF;
				else brr[cnt++] = fabs((float)(arr[j][1]-arr[i][1])/(float)(arr[j][0]-arr[i][0]));
			}
			brr[cnt] = -1;
			sort(brr, brr+cnt);
			for (int i = 1; i <= cnt; i++) {
				if (brr[i] == brr[i-1])
					num++;
				else num = 2;
				if (num > Max)
					Max = num;
			}
		}
		printf("%d\n", Max);
	}
	return 0;
}



你可能感兴趣的:(搜索)