uva-270-排序

题意:很多个点,问,最多有多少个点在同一条直线上

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <memory.h>
#include <sstream>

namespace cc
{
using std::cin;
using std::cout;
using std::endl;
using std::move;
using std::sort;
using std::string;
using std::stringstream;

class Point
{
  public:
    int x, y;
    Point(int x, int y) : x(x), y(y) {}
    Point() : x(0), y(0) {}
};

const int N = 701;

double k(int x1, int y1, int x2, int y2)
{
    int dy = y1 - y2;
    int dx = x1 - x2;
    if (dx == 0)
        return 1;
    return dy * 1.0 / dx;
}

void solve()
{
    int n;
    cin >> n;
    getchar();
    getchar();
    int t = 0;
    while (n--)
    {
        if (t != 0)
            cout << endl;
        ++t;
        string str;
        int total = 0;
        Point pp[N];
        while (getline(cin, str))
        {
            if (str.empty())
                break;
            stringstream strIn;
            strIn << str;
            int x, y;
            strIn >> x >> y;
            Point poin(x, y);
            pp[total++] = poin;
        }
        int max = 0x80000000;
        for (int base = 0; base < total; base++)
        {
            Point p = pp[base];
            double ks[N];
            int ki = 0;
            for (int i = 0; i < total; i++)
            {
                if (i == base)
                    continue;
                double kk = k(pp[base].x, pp[base].y, pp[i].x, pp[i].y);
                ks[ki++] = kk;
            }
            //sort
            sort(ks, ks + ki);
            int curMax = 2;
            double s = ks[0];
            for (int j = 1; j < ki; j++)
            {
                if (ks[j] == s)
                    ++curMax;
                else
                {
                    max = max > curMax ? max : curMax;
                    s = ks[j];
                    curMax = 2;
                }
            }
            max = max > curMax ? max : curMax;
        }
        cout<<max<<endl;
    }
}
} // namespace cc

int main()
{
#ifndef ONLINE_JUDGE
    freopen("/Users/caicai/in", "r", stdin);
#endif // !ONLINE_JUDGE

    cc::solve();

#ifndef ONLINE_JUDGE

    while (true)
        ;
#endif // !ONLINE_JUDGE
    return 0;
}
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10076371.html