如何在一个向量中找到最近的点对?

huangapple go评论78阅读模式
英文:

How to find the closest pair of points within a vector?

问题

我必须编写一个程序,从用户那里输入点的配对,并使用距离公式找到每个点的最近邻居。

根据给我的指示,我必须仅编辑main()函数并添加一个嵌套循环来比较每个点,但我不太确定如何做到这一点。

指导老师还编写了一个重载operator-函数,它返回距离,我认为我应该以某种方式使用它。

输出应该类似于这样:

# 示例输入 1

5 1 1 2 2 3 3 4 4 5 5

# 示例输出 1

(1,1) (2,2) (3,3) (4,4) (5,5) 
(1,1) 最近的是 (2,2)
(2,2) 最近的是 (1,1)
(3,3) 最近的是 (2,2)
(4,4) 最近的是 (3,3)
(5,5) 最近的是 (4,4)

以下是main.cpp代码:

void displayPoints(vector<Point> &points) {
  // 完成
  for (int i = 0; i < points.size(); i++) {
    cout << points[i] << " ";
  }
  cout << endl;
}

void createPointsList(vector<Point> &points) {
  int s;
  cin >> s;
  for (int i = 0; i < s; ++i) {
    int x, y;
    cin >> x >> y;
    points.push_back(Point(x, y));
  }
}

int main() {
  vector<Point> points;
  createPointsList(points);
  displayPoints(points);
  // 在这里添加找到最近邻的代码
  // 提示:需要一个嵌套循环来比较每个点与其他每个点
}

// 重载的运算符是:

// d=√((x2 – x1)² + (y2 – y1)²)
double Point::operator-(const Point& point) const { 
    return sqrt(pow(point.x - x, 2) + pow(point.y - y, 2)); 
}
英文:

I have to write a program that inputs pairs of points from the user and finds the closest neighbor to each point, using the distance formula.

From the instructions I've been given, I must edit only the main() function and add a nested loop to compare each point, but I'm not sure how to do this.

The instructor has also written a function to overload operator- that returns the distance, which I think I'm supposed to use, somehow.

The output should look something like this:

> # Example input 1
>
> none
&gt; 5 1 1 2 2 3 3 4 4 5 5
&gt;

>
> # Example output 1
>
> none
&gt; (1,1) (2,2) (3,3) (4,4) (5,5)
&gt; (1,1) nearest (2,2)
&gt; (2,2) nearest (1,1)
&gt; (3,3) nearest (2,2)
&gt; (4,4) nearest (3,3)
&gt; (5,5) nearest (4,4)
&gt;

This is the main.cpp code:

void displayPoints(vector&lt;Point&gt; &amp;points) {
  // Finish
  for (int i = 0; i &lt; points.size(); i++) {
    
    cout &lt;&lt; points[i] &lt;&lt; &quot; &quot;;
  }
  cout &lt;&lt;endl;
}

void createPointsList(vector&lt;Point&gt; &amp;points) {
  int s;
  cin &gt;&gt; s;
  for (int i = 0; i &lt; s; ++i) {
    int x, y;
    cin &gt;&gt; x &gt;&gt; y;
    points.push_back(Point(x, y));
  }
}

int main() {

  vector&lt;Point&gt; points;
  createPointsList(points);
  displayPoints(points);
  // Add code to find nearest here
  // Hint: will need a nested loop to compare each point with every other point

}

//The overloaded operator is :

// d=√((x2 – x1)&#178; + (y2 – y1)&#178;)
double Point::operator-(const Point&amp; point) const { 
    return (pow(point.x-x,2) + pow(point.y-y,2))); 
}

答案1

得分: 0

你可以使用嵌套循环,将每个值与其他值进行比较。

需要添加一个if语句,以防止将点与自身进行比较。

在循环中,找到最小的距离,并在外部循环结束时显示它的当前点。

解决方案相当简单:

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
#include &lt;limits&gt;

struct Point {
    double x{};
    double y{};
    friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const Point&amp; p) {
        return os &lt;&lt; &#39;(&#39; &lt;&lt; p.x &lt;&lt; &#39;,&#39; &lt;&lt; p.y &lt;&lt; &#39;)&#39;;
    }
    double operator-(const Point&amp; point) const {
        return (std::pow(point.x - x, 2) + std::pow(point.y - y, 2));
    }
};
void createPointsList(std::vector&lt;Point&gt;&amp; points) {
    int s;
    std::cin &gt;&gt; s;
    for (int i = 0; i &lt; s; ++i) {
        int x, y;
        std::cin &gt;&gt; x &gt;&gt; y;
        points.push_back(Point(x, y));
    }
}
void displayPoints(const std::vector&lt;Point&gt;&amp; points) {
    for (const Point&amp; p : points)
        std::cout &lt;&lt; p &lt;&lt; &#39;\n&#39;;
}

int main() {
    std::vector&lt;Point&gt; points;
    createPointsList(points);
    //displayPoints(points);

    // 添加代码来找到最近的点
    // 提示:需要嵌套循环来将每个点与其他点进行比较

    for (std::size_t i{}; i &lt; points.size(); ++i) {
        std::size_t nearestIndex{ i };
        double smallestDistance{ std::numeric_limits&lt;double&gt;::max()};

        for (std::size_t k{}; k &lt; points.size(); ++k) {
            if (i != k) {
                const double distance = points[k] - points[i];
                if (distance &lt; smallestDistance) {
                    smallestDistance = distance;
                    nearestIndex = k;
                }
            }
        }
        std::cout &lt;&lt; points[i] &lt;&lt; " 最近的是 " &lt;&lt; points[nearestIndex] &lt;&lt; &#39;\n&#39;;
    }
}

该程序的输入和输出如下图所示:

英文:

You could use a nested loop and compare each value with the other.

Need to add an if-statement to prevent to compare points to itself.

In your loop, find the smallest distance and show it for the current Point at the end of the outer loop.

The solution is rather straight forward:

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
#include &lt;limits&gt;

struct Point {
    double x{};
    double y{};
    friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const Point&amp; p) {
        return os &lt;&lt; &#39;(&#39; &lt;&lt; p.x &lt;&lt; &#39;,&#39; &lt;&lt; p.y &lt;&lt; &#39;)&#39;;
    }
    double operator-(const Point&amp; point) const {
        return (std::pow(point.x - x, 2) + std::pow(point.y - y, 2));
    }
};
void createPointsList(std::vector&lt;Point&gt;&amp; points) {
    int s;
    std::cin &gt;&gt; s;
    for (int i = 0; i &lt; s; ++i) {
        int x, y;
        std::cin &gt;&gt; x &gt;&gt; y;
        points.push_back(Point(x, y));
    }
}
void displayPoints(const std::vector&lt;Point&gt;&amp; points) {
    for (const Point&amp; p : points)
        std::cout &lt;&lt; p &lt;&lt; &#39;\n&#39;;
}

int main() {
    std::vector&lt;Point&gt; points;
    createPointsList(points);
    //displayPoints(points);

    // Add code to find nearest here
    // Hint: will need a nested loop to compare each point with every other point

    for (std::size_t i{}; i &lt; points.size(); ++i) {
        std::size_t nearestIndex{ i };
        double smallestDistance{ std::numeric_limits&lt;double&gt;::max()};

        for (std::size_t k{}; k &lt; points.size(); ++k) {
            if (i != k) {
                const double distance = points[k] - points[i];
                if (distance &lt; smallestDistance) {
                    smallestDistance = distance;
                    nearestIndex = k;
                }
            }
        }
        std::cout &lt;&lt; points[i] &lt;&lt; &quot; nearest &quot; &lt;&lt; points[nearestIndex] &lt;&lt; &#39;\n&#39;;
    }
}

Input and output of this program:

如何在一个向量中找到最近的点对?

huangapple
  • 本文由 发表于 2023年3月4日 00:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定