英文:
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
> 5 1 1 2 2 3 3 4 4 5 5
>
>
> # Example output 1
>
> none
> (1,1) (2,2) (3,3) (4,4) (5,5)
> (1,1) nearest (2,2)
> (2,2) nearest (1,1)
> (3,3) nearest (2,2)
> (4,4) nearest (3,3)
> (5,5) nearest (4,4)
>
This is the main.cpp
code:
void displayPoints(vector<Point> &points) {
// Finish
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);
// 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)² + (y2 – y1)²)
double Point::operator-(const Point& point) const {
return (pow(point.x-x,2) + pow(point.y-y,2)));
}
答案1
得分: 0
你可以使用嵌套循环,将每个值与其他值进行比较。
需要添加一个if
语句,以防止将点与自身进行比较。
在循环中,找到最小的距离,并在外部循环结束时显示它的当前点。
解决方案相当简单:
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
struct Point {
double x{};
double y{};
friend std::ostream& operator << (std::ostream& os, const Point& p) {
return os << '(' << p.x << ',' << p.y << ')';
}
double operator-(const Point& point) const {
return (std::pow(point.x - x, 2) + std::pow(point.y - y, 2));
}
};
void createPointsList(std::vector<Point>& points) {
int s;
std::cin >> s;
for (int i = 0; i < s; ++i) {
int x, y;
std::cin >> x >> y;
points.push_back(Point(x, y));
}
}
void displayPoints(const std::vector<Point>& points) {
for (const Point& p : points)
std::cout << p << '\n';
}
int main() {
std::vector<Point> points;
createPointsList(points);
//displayPoints(points);
// 添加代码来找到最近的点
// 提示:需要嵌套循环来将每个点与其他点进行比较
for (std::size_t i{}; i < points.size(); ++i) {
std::size_t nearestIndex{ i };
double smallestDistance{ std::numeric_limits<double>::max()};
for (std::size_t k{}; k < points.size(); ++k) {
if (i != k) {
const double distance = points[k] - points[i];
if (distance < smallestDistance) {
smallestDistance = distance;
nearestIndex = k;
}
}
}
std::cout << points[i] << " 最近的是 " << points[nearestIndex] << '\n';
}
}
该程序的输入和输出如下图所示:
英文:
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 <iostream>
#include <vector>
#include <cmath>
#include <limits>
struct Point {
double x{};
double y{};
friend std::ostream& operator << (std::ostream& os, const Point& p) {
return os << '(' << p.x << ',' << p.y << ')';
}
double operator-(const Point& point) const {
return (std::pow(point.x - x, 2) + std::pow(point.y - y, 2));
}
};
void createPointsList(std::vector<Point>& points) {
int s;
std::cin >> s;
for (int i = 0; i < s; ++i) {
int x, y;
std::cin >> x >> y;
points.push_back(Point(x, y));
}
}
void displayPoints(const std::vector<Point>& points) {
for (const Point& p : points)
std::cout << p << '\n';
}
int main() {
std::vector<Point> 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 < points.size(); ++i) {
std::size_t nearestIndex{ i };
double smallestDistance{ std::numeric_limits<double>::max()};
for (std::size_t k{}; k < points.size(); ++k) {
if (i != k) {
const double distance = points[k] - points[i];
if (distance < smallestDistance) {
smallestDistance = distance;
nearestIndex = k;
}
}
}
std::cout << points[i] << " nearest " << points[nearestIndex] << '\n';
}
}
Input and output of this program:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论