uva 270 Lining Up(暴力+ 几何)

来源:互联网 发布:法兰克系统仿真软件 编辑:程序博客网 时间:2024/05/05 01:10

题目连接:270 - Lining Up


题目大意:给出一系列点,要求找出共线的点数最大的值。


解题思路:暴力,遍历每两个点确定的直线上又多少点, 每次更新最大值。

三个点共线的性质:A(X1,Y1),B(X2,Y2),C(X3,Y3);这个时候有(Y2-Y1)/(X2-X1) = (Y3-Y2)/(X3-X2)

最好转化成(Y2 - Y1) * (X3 - X2) =  (Y3 - Y2) * (X2 - X1).


#include <stdio.h>#include <string.h>#include <stdlib.h>int max(const int &a, const int &b) { return a > b ? a : b;}const int N = 1005;int n, x[N], y[N];int find (int p, int q, int c) {    int cnt = 0;    for (int i = c + 1; i < n; i++) {if (p * (y[i] - y[c]) == q * (x[i] - x[c]))cnt++;    }    return cnt;}int solve() {    int Max = 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;    Max = max(Max, cur);}    }    return Max;}int main() {    int cas;    char str[N];    scanf("%d%*c%*c", &cas);    while (cas--) {// Init;memset(x, 0, sizeof(x));memset(y, 0, sizeof(y));n = 0;// Read;while (gets(str)) {    if (!str[0] && n)break;    sscanf(str, "%d%d", &x[n], &y[n]);    n++;}printf("%d\n", solve());if (cas)    printf("\n");    }    return 0;}


原创粉丝点击