这是在数组中查找最接近的点对的程序。
对于最近点之间的距离
Begin Declare function Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) to the double datatype. Declare Minimum to the double datatype. Initialize Minimum = dist. for (int i = 0; i < s; ++i) for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j) if (Distance(stp[i],stp[j]) < Minimum) then Minimum = Distance(stp[i], stp[j]). pnt1.poi1 = stp[i].poi1, pnt1.poi2 = stp[i].poi2. pnt2.poi1 = stp[j].poi1, pnt2.poi2 = stp[j].poi2. Return Minimum. End.
要计算最小距离-
Begin Declare function Closest_dist(poi P[], poi stp[], int n, poi &pnt1, poi &pnt2) to the double datatype. Declare static object pt1, pt2, pt3, pt4 of poi structure. if (n <= 3) then return S_Distance(P, n, pt1, pt2). Declare medium to the integer datatype. Initialize midium = n/2. Declare object mediumPoint of poi structure. Initialize midiumPoint = P[midium]. Declare D_Left to the double datatype. Initialize D _Left = Closest_dist(P, stp, midium, pt1, pt2). Declare D_Right to the double datatype. Initialize D_Right = Closest_dist(P + midium, stp, n-midium, pt3, pt4). if(D_Left < D_Right) then pnt1.poi1 = pt1.poi1; pnt1.poi2 = pt1.poi2. pnt2.poi1 = pt2.poi1; pnt2.poi2 = pt2.poi2. else pnt1.poi1 = pt3.poi1; pnt1.poi2 = pt3.poi2; pnt2.poi1 = pt4.poi1; pnt2.poi2 = pt4.poi2; Declare min_dist to the double datatype. Initialize min_dist = Minimum(D_Left, D_Right). Declare j to the integer datatype. initialize j = 0. for (int i = 0; i < n; i++) if (abs(P[i].poi1 - midiumPoint.poi1) < min_dist) then stp[j++] = P[i]. Declare min_dist_strip, F_Min to the double datatype. Initalize min_dist_strip = Closest_dist_Spoint(stp, j, min_dist, pt1, pt2). Initialize F_Min = min_dist. if(min_dist_strip < min_dist) then pnt1.poi1 = pt1.poi1; pnt1.poi2 = pt1.poi2; pnt2.poi1 = pt2.poi1; pnt2.poi2 = pt2.poi2; F_Min = min_dist_strip; Return F_Min. End.
#include <iostream> #include <cfloat> #include <cstdlib> #include <cmath> using namespace std; struct poi { double poi1, poi2; }; inline int Comp_poi1(const void* x, const void* b) { poi *p1 = (poi *)x, *pnt2 = (poi *)b; return (p1->poi1 - pnt2->poi1); } inline int Comp_poi2(const void* x, const void* y) { poi *pnt1 = (poi *)x, *pnt2 = (poi *)y; return (pnt1->poi2 - pnt2->poi2); } inline double Distance(poi pnt1, poi pnt2) { // Calculate the distance between two points return sqrt( (pnt1.poi1 - pnt2.poi1)*(pnt1.poi1 - pnt2.poi1) + (pnt1.poi2 - pnt2.poi2)*(pnt1.poi2 - pnt2.poi2) ); } double S_Distance(poi P[], int n, poi &pnt1, poi &pnt2) { double min = DBL_MAX; for (int i = 0; i < n; ++i) for (int j = i+1; j < n; ++j) if (Distance(P[i], P[j]) < min) { min = Distance(P[i], P[j]); pnt1.poi1 = P[i].poi1, pnt1.poi2 = P[i].poi2; pnt2.poi1 = P[j].poi1, pnt2.poi2 = P[j].poi2; } return min; } inline double Minimum(double poi1, double poi2) { // Find minimum between two values return (poi1 < poi2)? poi1 : poi2; } double Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) { // Calculate distance beween the closest points double Minimum = dist; // Initialize the minimum distance as dist qsort(stp, s, sizeof(poi), Comp_poi2); for (int i = 0; i < s; ++i) for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j) if (Distance(stp[i],stp[j]) < Minimum) { Minimum = Distance(stp[i], stp[j]); pnt1.poi1 = stp[i].poi1, pnt1.poi2 = stp[i].poi2; pnt2.poi1 = stp[j].poi1, pnt2.poi2 = stp[j].poi2; } return Minimum; } double Closest_dist(poi P[], poi stp[], int n, poi &pnt1, poi &pnt2) { // Calculate smallest distance. static poi pt1, pt2, pt3, pt4; if (n <= 3) return S_Distance(P, n, pt1, pt2); int medium = n/2; // Calculate the mid point poi mediumPoint = P[medium]; double D_Left = Closest_dist(P, stp, medium, pt1, pt2); // D_Left: left of medium point double D_Right = Closest_dist(P + medium, stp, n-medium, pt3, pt4); // D_Right: right side of the medium point if(D_Left < D_Right) { pnt1.poi1 = pt1.poi1; pnt1.poi2 = pt1.poi2; // Store the pair that has smaller distance pnt2.poi1 = pt2.poi1; pnt2.poi2 = pt2.poi2; } else { pnt1.poi1 = pt3.poi1; pnt1.poi2 = pt3.poi2; pnt2.poi1 = pt4.poi1; pnt2.poi2 = pt4.poi2; } double min_dist = Minimum(D_Left, D_Right); int j = 0; for (int i = 0; i < n; i++) if (abs(P[i].poi1 - mediumPoint.poi1) < min_dist) stp[j++] = P[i]; double min_dist_strip = Closest_dist_Spoint(stp, j, min_dist, pt1, pt2); double F_Min = min_dist; if(min_dist_strip < min_dist) { pnt1.poi1 = pt1.poi1; pnt1.poi2 = pt1.poi2; pnt2.poi1 = pt2.poi1; pnt2.poi2 = pt2.poi2; F_Min = min_dist_strip; } return F_Min; } int main() { poi P[] = {{4, 1}, {15, 20}, {30, 40}, {8, 4}, {13, 11}, {5, 6}}; poi pnt1 = {DBL_MAX, DBL_MAX}, pnt2 = {DBL_MAX, DBL_MAX}; // Closest pair of points in array int n = sizeof(P) / sizeof(P[0]); qsort(P, n, sizeof(poi), Comp_poi1); poi *stp = new poi[n]; cout << "The closest distance of point in array is: " << Closest_dist(P, stp, n, pnt1, pnt2) << endl; cout << "The closest pair of point in array: (" << pnt1.poi1 << "," << pnt1.poi2 << ") and (" << pnt2.poi1 << "," << pnt2.poi2 << ")" << endl; delete[] stp; return 0; }
输出结果
The closest distance of point in array is: 3.60555 The closest pair of point in array: (13,11) and (15,20)
本文向大家介绍Java程序从两个排序的数组中查找最接近的一对,包括了Java程序从两个排序的数组中查找最接近的一对的使用技巧和注意事项,需要的朋友参考一下 为了从两个排序的数组中找到最接近的一对,Java代码如下: 示例 输出结果 一个名为Demo的类包含一个名为closest_pair的函数,该函数遍历两个数组并检查哪个和与前面指定的数字非常接近。数组中的这对将作为输出返回。在main函数中,定
问题内容: 我希望能够在数字数组中找到最接近的较小值。例如,如果我有: 我正在寻找小于以下值的最接近值: 该函数将返回: 另外,如果我传递的数字大于数组中的最大值,则它应返回最大值。如果我传递的数字小于最小值,则应返回nil。 我尝试使用数组上的函数执行此操作,但是单独执行此操作不会产生我想要的结果,因为我需要这样的东西: 但不幸的是,这是无效的。有什么建议?我知道可以使用while循环轻松完成此
问题内容: 是否有numpy-thonic方法(例如函数)在数组中查找最接近的值? 例: 问题答案:
问题内容: 我有两个2d numpy数组:x_array包含x方向上的位置信息,y_array包含y方向上的位置。 然后,我有一长串x,y点。 对于列表中的每个点,我需要找到最接近该点的位置(在数组中指定)的数组索引。 我已经根据这个问题天真的产生了一些有效的代码: 在numpy数组中找到最接近的值 即 我正在大型数据集上执行此操作,并且真的想加快速度。谁能优化这个? 谢谢。 更新:根据@silv
问题内容: 如何为给定的目标值搜索和查找数组中最接近的值? 假设我有这个示例数组: 例如,当我用目标值0搜索时,该函数应返回0;否则,该函数将返回0。当我搜索3时,它将返回5;当我搜索14时,它将返回12。 问题答案: 将您要搜索的数字作为第一个参数,将数字数组作为第二个参数:
本文向大家介绍在C ++中查找数组中的分区点,包括了在C ++中查找数组中的分区点的使用技巧和注意事项,需要的朋友参考一下 在本教程中,我们将在数组中找到分区点,该数组中所有剩余的元素都很小,而所有剩余的元素都很大。 让我们看看解决问题的步骤。 初始化数组。 遍历数组。 从0迭代到I,然后检查每个值是否小于当前值。 从I迭代到n,并检查每个值是否大于当前值。 如果机器人满足条件,则返回该值。 打印