如果陈述不承认真实条件? [英] If statement not recognizing true conditions?

查看:64
本文介绍了如果陈述不承认真实条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用这种二进制搜索算法时遇到了麻烦.这是变量的说明.

I'm having trouble with this binary search algorithm. Here are explanations of the variables.

value:在数组中搜索的数字

value: the number being searched within the array

values []:正在搜索的数组

values[]: the array that is being searched

n:数组中元素的数量

n: number of elements in the array

high:要搜索的数组部分的最高元素(按零索引位置)

high: highest element (by zero indexed position) of portion of the array being searched

low:最低元素(按零索引位置)被搜索的数组部分

low: lowest element (by zero indexed position) the portion of the array being searched

我的问题不是递归.正在搜索的数组部分以值"为中心,并且满足以下标识的条件.问题是我的if语句似乎无法识别出它们是.我知道条件已经得到满足,因为当我为每个递归打印出values [high],values [middle]和values [low]时,就表明它们是满足的.

My problem isn't the recursion. The portion of the array being searched centers around "value" and conditions identified below are being met. the problem is that my if statements don't seem to be recognizing that they are. I know the conditions are being met because when I print out values[high], values[middle], and values[low] for each recursion it shows that they are.

int search(int value, int values[], int n, int high, int low)
 {   
   if (n <= 0)
   {
    return 1;
   }

   int middle = (high + low) / 2;

     ///condition #1
   if (value == values[middle])
   {
     return 0;
   }

   //conditions #2 and #3 (account for the maxes and mins of the array because the operation to find middle truncates)
  else if ( values[middle]==values[low] || values[middle]==values[high])
    {
     return 0;
    }

  else if (value > values[middle])
   {
        low = middle;
        search(value, values, n, high, low);
   }

  else if (value < values[middle])
   {
      high = middle;
      search(value, values, n, high, low);
   }

    return 2;
   } 

这是怎么了?

推荐答案

仔细查看以下代码:

else if (value > values[middle])
{
     low = middle;
     search(value, values, n, high, low);
}

else if (value < values[middle])
{
   high = middle;
   search(value, values, n, high, low);
}

请注意,在这些情况下,您递归调用search函数,但是对返回值不做任何事情.这意味着search返回的任何值都将被丢弃,并且代码将照常继续执行,最终返回2.

Notice that in these cases you call the search function recursively, but you don't do anything with the return value. This means that whatever value returned by search is discarded and the code continues on as usually, ultimately returning 2.

要解决此问题,请添加以下return语句:

To fix this, add in these return statements:

else if (value > values[middle])
{
     low = middle;
     return search(value, values, n, high, low);
}

else if (value < values[middle])
{
   high = middle;
   return search(value, values, n, high, low);
}

通常来说,如果您怀疑if语句条件没有触发,则值得使用调试器逐步进行操作.这样做可能会导致您注意到您(1)正确地递归调用该函数,但(2)返回并丢弃返回的值.

Generally speaking, if you suspect that an if statement condition isn't firing, it's worth slowly stepping through things with a debugger. Doing so would likely lead you to notice that you were (1) calling the function recursively correctly but (2) returning and discarding the returned value.

这里的代码可能还有其他问题,但这肯定是您需要解决的问题.

There may be other issues with the code here, but this is certainly something that you're going to need to address.

这篇关于如果陈述不承认真实条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆