• 三维坐标判共线


    #include<vector>
    #include<map>
    #include<iostream>
    using namespace std;
    struct Point {
        int x;
        int y;
        int z;
        Point() : x(0), y(0),z(0) {}
        Point(int a, int b,int c) : x(a), y(b),z(c) {}
    };
    int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
    int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
    int max(int a, int b) { return a > b ? a : b; }
    int maxPoints(vector<Point>& points) 
    {
        int ans = 0;
        for (int i = 0; i < points.size(); i++) {
            int cnt = 0,overLap=0;
            map<pair<pair<int, int>, int>,int> line;
            for (int j = i + 1; j < points.size(); j++) {
                int dx = points[i].x - points[j].x;
                int dy = points[i].y - points[j].y;
                int dz = points[i].z - points[j].z;
                //判断两点是否重合
                if (dx == 0 && dy == 0 && dz==0) {
                    ++overLap;
                    continue;
                }
                //计算最简方向向量并存入map容器中,同时更新cnt
                int g = gcd(dx, dy,dz);
                dx /= g;
                dy /= g;
                dz /= g;
    
                cnt=max(cnt,++line[pair<pair<int, int>,int>(pair<int,int>(dx, dy),dz)]);
            }
            //统计该基准点下的最大共线点数量
            //cnt+重合点+基准点自身
            ans = max(ans, cnt + overLap + 1);
        }
        return ans;
    }
    int main()
    {
        int n;
        cin >> n;
        vector<Point> points(n);
        for (int i = 0; i < n; i++) 
            cin >> points[i].x >> points[i].y>>points[i].z;
        cout << maxPoints(points) << endl;
    }

    链接:https://www.nowcoder.com/questionTerminal/7f1490a737024704a3f2b7aad476e3ac

    #include<vector>#include<map>#include<iostream>using namespace std;struct Point {int x;int y;int z;Point() : x(0), y(0),z(0) {}Point(int a, int b,int c) : x(a), y(b),z(c) {}};int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }int max(int a, int b) { return a > b ? a : b; }int maxPoints(vector<Point>& points) {int ans = 0;for (int i = 0; i < points.size(); i++) {int cnt = 0,overLap=0;map<pair<pair<int, int>, int>,int> line;for (int j = i + 1; j < points.size(); j++) {int dx = points[i].x - points[j].x;int dy = points[i].y - points[j].y;int dz = points[i].z - points[j].z;//判断两点是否重合if (dx == 0 && dy == 0 && dz==0) {++overLap;continue;}//计算最简方向向量并存入map容器中,同时更新cntint g = gcd(dx, dy,dz);dx /= g;dy /= g;dz /= g;
    cnt=max(cnt,++line[pair<pair<int, int>,int>(pair<int,int>(dx, dy),dz)]);}//统计该基准点下的最大共线点数量//cnt+重合点+基准点自身ans = max(ans, cnt + overLap + 1);}return ans;}int main(){int n;cin >> n;vector<Point> points(n);for (int i = 0; i < n; i++) cin >> points[i].x >> points[i].y>>points[i].z;cout << maxPoints(points) << endl;}

  • 相关阅读:
    PHPCMS V9生成静态地址绝对路径改为相对路径的方法
    python常识系列21-->一个不能不用的python日志模块**nb_log**
    python常识系列20-->python利用xlutils修改表格内容
    单元测试框架杂记02-->unittest结合requests时报一个警告
    修改element的NavMenu 导航菜单组件样式
    算法-04 | 贪心算法
    算法-05 | 二分查找
    数据结构-07| 堆
    数据结构-06| 字典树| 并查集
    数据结构-05| 布隆过滤器| 缓存机制
  • 原文地址:https://www.cnblogs.com/acmLLF/p/14716007.html
Copyright © 2020-2023  润新知