【问题标题】:Random garbage ouput when trying to find the minimum distance between points in an array尝试查找数组中点之间的最小距离时的随机垃圾输出
【发布时间】:2019-07-13 13:30:29
【问题描述】:

有什么大惊小怪的?

我试图找到点之间的最小距离(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.

我在这里做错了什么?也欢迎替代(和更有效的算法)!提前致谢! :)

【问题讨论】:

  • 您在以下行中有错字:int j = i + 2; i &lt;= num_pairs; j += 2,应为j 而不是i。在访问vector 时使用at 而不是[],在at 越界访问时会出现异常。
  • @rafix07 是的!感谢您指出了这一点!我将编辑代码。现在,输出来了,但它是一个随机垃圾:-2147483648。
  • @rafix07 另外,我意识到我一定是失明了,我所有的脑细胞一定在过去 1 小时左右都死了。

标签: c++ c++14 closest-points


【解决方案1】:

在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)并跳过此迭代的情况。然后您将计算所有距离。

【讨论】:

  • 这个问题实际上是一个赋值,问题陈述有x1, x2这样的符号。但是,您的评估是正确的,因为 x2 最好读作 y1,对应于坐标轴。
【解决方案2】:

除了rafix07 建议的修复之外,要使此源代码正常工作,还必须进行另一项更改:

for (int j = i + 2; j &lt;= num_pairs; j += 2)

实际上应该是:

for (int j = i + 2; j &lt;= num_pairs + 2; j += 2)

这是因为i 对于4 对的输入最多可以达到4 的值(数组大小:0 -> 7)。由于j 也依赖于i,所以在i 上总共执行了4 增量。所以i 最多只能是4,所以x1 = 4, x2 = 5, y1 = 6 和y2 = 7。另一方面,j 对于4 对的输入最多可以是6(数组大小:0 -> 7)。这是因为如果i == 4 和j == 6,那么y1 = 6 和y2 = 7,这是向量points 中的最后一个索引。

【讨论】:

    猜你喜欢
    • 2014-01-21
    • 2021-10-24
    • 2013-01-24
    • 1970-01-01
    • 2017-01-02
    • 2022-06-17
    • 1970-01-01
    • 2012-09-01
    • 2013-12-09
    相关资源
    最近更新 更多