poj1118 简单几何(同一条直线上的最多点数)

2019-11-05  本文已影响0人  暖昼氤氲
/*
Time:2019.11.4
Author: Goven
type:枚举 (暴力)
err: 用乘积替代斜率,就能表示斜率是无穷的情况 
ref:https://blog.csdn.net/qq_23100787/article/details/48265429
*/
#include<iostream>
#include<algorithm>
#define DOUBLE_MAX 123456789
using namespace std;

int x[705], y[705];

int main()
{
    int n;
    while (cin >> n && n) {
        for (int i = 0; i < n; i++) {
            cin >> x[i] >> y[i];
        }
        int dx, dy, sum = 2, cnt;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                dx = x[i] - x[j];
                dy = y[i] - y[j];
                cnt = 2;
                for (int k = j + 1; k < n; k++) {
                    if (dx * (y[i] - y[k]) == (x[i] - x[k]) * dy) cnt++;
                }
                if (sum < cnt) sum = cnt;
            }
            
        }
        cout << sum << endl;
    }
    
    return 0;
}

/*错误版--斜率无穷无法表示 
#include<iostream>
#include<algorithm>
#define DOUBLE_MAX 123456789
using namespace std;

int x[705], y[705];
double d[705];

int main()
{
    int n;
    while (cin >> n && n) {
        for (int i = 0; i < n; i++) {
            cin >> x[i] >> y[i];
        }
        int sum = 1;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (x[i] - x[j] == 0) d[j] = DOUBLE_MAX;
                else d[j] = 1.0 * (y[i] - y[j]) / (x[i] - x[j]);
            }
            sort(d + i + 1, d + n);
            int cnt = 1;
            for (int j = i + 2; j < n; j++) {
                if (d[j - 1] == d[j]) cnt++;
                else {
                    if (sum < cnt) sum = cnt;
                    cnt = 1;
                }
            }
        }
        cout << ++sum << endl;
    }
    return 0;
}
*/
上一篇下一篇

猜你喜欢

热点阅读