zoukankan      html  css  js  c++  java
  • K points to origin

    Given a bounch of points, ask to find K closest point to origin. 

    This question can be changed. For example, here is origin, but there might be another point. 

    Here is just a basic algorithm.

    //  main.cpp

    //  k points to origin

    //  Created by Xiaohe Huang on 10/19/15.

    //  Copyright © 2015 Xiaohe Huang. All rights reserved.

    //

    #include <iostream>

    #include<algorithm>

    #include<queue>

    using namespace std;

    struct Cpoint

    {

        double x;

        double y;

    };

    struct cmp

    {

    public:

        bool operator()(Cpoint n1,Cpoint n2)

        {

            return (n1.x)*(n1.x)+(n1.y)*(n1.y)<(n2.x)*(n2.x)+(n2.y)*(n2.y);

        }

    };

    int main(int argc, const char * argv[]) {

        // insert code here...

        Cpoint points[5];

        points[0]={9,9};

        points[1]={8,8};

        points[2]={7,7};

        points[3]={6,6};

        points[4]={5,5};

        priority_queue<Cpoint,vector<Cpoint>,cmp> pq;

        for(int i;i<5;i++)

        {

            if(i<3)

                pq.push(points[i]);

            else

            {

                if((points[i].x)*(points[i].x)+(points[i].y)+(points[i].y)<(pq.top().x+pq.top().x)+(pq.top().y+pq.top().y))

                {

                    pq.pop();

                    pq.push(points[i]);

                }

            }

        }

         return 0;

    }

  • 相关阅读:
    前端构建工具gulp入门教程
    写网站经常需要用到的代码汇总
    前端需要掌握的知识
    百度搜索功能
    Photoshop教程
    1.电脑系统重装
    ansible安装二进制kubernetes-1.14.1
    Ansible-基础
    kubernetes之监控Operator部署Prometheus(三)
    kubernetes之监控Prometheus实战--邮件告警--微信告警(二)
  • 原文地址:https://www.cnblogs.com/CathyXiaohe/p/4985284.html
Copyright © 2011-2022 走看看