尝试查找数组中点之间的最小距离时的随机垃圾输出
Random garbage ouput when trying to find the minimum distance between points in an array
有什么大惊小怪的?
我正在尝试找到点之间的最小距离(2D - 平面中 2 个点之间的距离:与(x1, y1) to (y1, y2))
的距离,它们存储在数组arr
中,然后计算并返回这些距离的最小值。
但是,问题是我的源代码产生了随机垃圾输出。
这个想法是用公式(x1, y1) and (x2, y2)
得到点之间的距离:sqrt((x1 - x2)^2 + (y1 - y2)^2)
. 为此,我为每个迭代选择 4 个元素:x1 = arr[0], x2 = arr[1], y1 = arr[2], y2 = arr[3]
.x1
和x2
在每次迭代(i
)中保持不变,而x1, x2
和y1, y2
之间的距离(j
的每个唯一迭代都有所不同)。最后,选择两点之间的最短距离并返回给main()
。
我做了什么来解决这个烂摊子?
在源代码中包含调试语句表明罪魁祸首是随机垃圾值(从字面上看,这甚至不应该存在!
另一个罪魁祸首是sqrt(arg)
给出了一个随机的垃圾值。例如,在计算(4, 4)
和(1, 100)
之间的距离时,结果是sqrt(0 + (-99)^2) = 99
。但相反,它输出-2147483648
.
这是我的代码:
#include<iostream>
#include<vector>
#include<cmath>
using std::sqrt;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int dist_cal(vector<int>&, int);
int main()
{
int num_pairs = -1;
cout << "Enter the number of pairs of point co-ordinates (x, y) that you want to enter: " << endl;
cin >> num_pairs;
vector<int> points;
cout << "Now enter the (x, y) co-ordinate pairs: " << endl;
for (int i = 0; i < num_pairs * 2; i++)
{
int buff;
cin >> buff;
points.push_back(buff);
}
cout << "The minimum distance between the array of points entered is " << dist_cal(points, num_pairs) << "." << endl;
return 0;
}
int dist_cal(vector<int>& arr, int num_pairs)
{
int min_distance = -1, temp_distance = -1, x1, x2, y1, y2, itr_count = 0;
for (int i = 0; i <= num_pairs; i += 2)
{
x1 = arr[i + 0];
x2 = arr[i + 1];
for (int j = i + 2; j <= num_pairs; j += 2)
{
y1 = arr[j + 0];
y2 = arr[j + 1];
temp_distance = sqrt((x1 - x2)^2 + (y1 - y2)^2);
if (itr_count == 0)
{
min_distance = temp_distance;
itr_count++;
}
if (min_distance > temp_distance)
{
min_distance = temp_distance;
}
}
}
return min_distance;
}
我知道这种方法很幼稚,而且是O(n^2),但是要转向更快的算法,我必须首先用最基本的精神健全方法来解决它。
对于输入:
4
4 4
7 8
1 100
4 4
输出应0
。
实际输出如下:The minimum distance between the array of points entered is -2147483648.
我在这里做错了什么?也欢迎替代(和更有效的算法)!提前感谢!:)
在 C++^
表示 XOR 位运算,如果你想x1-x2
升到 2 的幂,你可以写:(x1-x2) * (x1 - x2)
或使用std::pow
函数。
所以这个
sqrt((x1 - x2)^2 + (y1 - y2)^2);
应该是:
sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
另一个问题,sqrt
返回实数,因此min_distance
和temp_distance
应该是double
或float
。
您的矢量以以下形式保存坐标:x(i),y(i),..
所以这个读
x1 = arr[i + 0];
x2 = arr[i + 1];
是错误的,应该是:
x1 = arr[i + 0];
y1 = arr[i + 1];
在内部循环中执行相同的操作。
此外,您的内部循环应该从索引0
开始。并且您必须检测给定p
点distance(p,p)
计算的情况(它始终为 0)并跳过此迭代。然后,您将计算所有距离。
除了 rafix07 建议的修复之外,要使此源代码正常工作,还必须进行另一项更改:
for (int j = i + 2; j <= num_pairs; j += 2)
实际上应该是:
for (int j = i + 2; j <= num_pairs + 2; j += 2)
这是因为i
最多可以达到4
对输入的值4
(数组大小:0
->7
)。由于j
也取决于i
,因此总共4
增量在i
上执行。所以i
最多必须4
,这样x1 = 4, x2 = 5, y1 = 6
,y2 = 7
。另一方面,对于4
对的输入,j
最多可以6
(数组大小:0
->7
)。这是因为如果i == 4
和j == 6
,则y1 = 6
和y2 = 7
,这是向量points
中的最后一个索引。
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 循环中的随机函数
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 在c++构造函数中使用随机字符串生成器
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 用C++程序计算圆锥体的体积、球体的体积、八边形的面积和两点之间的距离
- 将字符随机转换为大写的函数
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 如何生成一个随机的 n 位数,其中 n 是任意的
- 如何生成统一的随机二进制数以成对计算C++中的汉明距离?
- 尝试查找数组中点之间的最小距离时的随机垃圾输出
- 如何在保持最小距离的同时沿有限线随机采样
- Python的随机森林距离无法构建(g++)
- 如何让敌人在一定距离内随机循环并跟随玩家?c++主机游戏