用 C++ 找到距離原點最近的 K 個點
假設我們有一組點。我們的任務是找到距離原點最近的 K 個點。假定這些點為(3, 3)、(5, -1)、(-2, 4)。那麼最接近的兩個點(K = 2)是(3, 3)、(-2, 4)。
要解決這個問題,我們將根據點的歐幾里得距離對點列表進行排序,然後從排序列表中取出最前面的 K 個元素。它們就是距離最近的 K 個點。
示例
#include<iostream> #include<algorithm> using namespace std; class Point{ private: int x, y; public: Point(int x = 0, int y = 0){ this->x = x; this->y = y; } void display(){ cout << "("<<x<<", "<<y<<")"; } friend bool comparePoints(Point &p1, Point &p2); }; bool comparePoints(Point &p1, Point &p2){ float dist1 = (p1.x * p1.x) + (p1.y * p1.y); float dist2 = (p2.x * p2.x) + (p2.y * p2.y); return dist1 < dist2; } void closestKPoints(Point points[], int n, int k){ sort(points, points+n, comparePoints); for(int i = 0; i<k; i++){ points[i].display(); cout << endl; } } int main() { Point points[] = {{3, 3},{5, -1},{-2, 4}}; int n = sizeof(points)/sizeof(points[0]); int k = 2; closestKPoints(points, n, k); }
輸出
(3, 3) (-2, 4)
廣告