递归问题 [英] Problem with recursion

查看:66
本文介绍了递归问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用递归找到数组中的最大数字,但它一直将索引返回到0.



我想知道这是否正确这样做的方法:

  public   static   double  findMax( double  [] number, int  count){
double max = numbers [ 0 ];
double current = numbers [count- 1 ];

if (count-1 == 0){
return 最大;
}
其他 {

如果(当前> ; max){
max = current;
}

return findMax(numbers,count- 1 ) ;

解决方案

提示#1:

因为你打电话给 findMax 递归作为函数中的最后一个东西,这是尾递归。尾部递归可以用整个函数体周围的循环替换。如果你这样做,那么错误应该更加明显。 (我知道你说你必须使用递归。这只是分析代码行为的练习。)

  public   static   double  findMax( double  [] number, int  count){
do
{
double max = numbers [ 0 ];
double current = numbers [count- 1 ];

if (count- 1 == 0 ){ // 为什么不是if(count == 1)??
return max;
}
其他 {

if (当前> max){
max = current;
}

// return findMax(numbers,count-1);
count = count - 1 ;
}
} while (true);
}



提示#2:每次变量 max 会发生什么?


如果你查看你的代码,你永远不会返回max,你无条件地返回递归的结果......并且因为项目零是最后一个验证的,它将返回这样的值。



我没有测试它,所以它可能会遗漏一些东西,但可能会有更正:



  public   static   double  findMax( double  []个数字, int  count){
if (count < = 0
throw new ArgumentOutOfRangeException( 计数< /跨度>);

double current = numbers [count-1];

if (count == 1 ){
return current;
}
else {

double recursiveMax = findMax(numbers,count-1)
if (recursiveMax> current){
return recursiveMax;
}

返回当前;
}
}


  public   static   double  findMax( double  []数字, int  count)
{
double c = numbers [count- < span class =code-digit> 1 ];
if (count == 1)
return c;
else
{
double f = findMax(number,count - 1 );
返回 c> F ? c:f;
}
}


I am trying to find the max number in the array using recursion, but it keeps returning the index at 0.

I was wondering if this is the right way to go about doing it:

public static double findMax(double[] numbers, int count){
       double max = numbers[0];
       double current= numbers[count-1];

      if(count-1==0){
          return max;
      }
        else{

          if(current>max){
              max= current;
          }

          return  findMax(numbers,count-1);

解决方案

Hint #1:
Since you are calling findMax recursively as the last thing in the function, this is "tail recursion". And tail recursion can be replaced with a loop around the entire function body. If you do this it should become more apparent what is wrong. (I know you said you must use recursion. This is just an exercise in analyzing the behavior of your code.)

public static double findMax(double[] numbers, int count){
  do
  {
    double max = numbers[0];
    double current= numbers[count-1];
 
    if (count-1 == 0) {  // why not if (count == 1) ??
       return max;
    }
    else{
 
      if (current > max){
        max = current;
      }
 
      // return  findMax(numbers,count-1);
      count = count - 1;
    }
  } while (true);
}


Hint #2: What is happening to the variable max each time?


If you look at your code, you are never returning max, you are returning the result of your recursion unconditionally... and as the item zero is the last one verified, it will return such value.

I am not testing it, so it may miss something, but a correction could be:

public static double findMax(double[] numbers, int count){
      if (count <= 0)
        throw new ArgumentOutOfRangeException("count");

      double current = numbers[count-1];
 
      if(count==1){
          return current;
      }
        else{
 
          double recursiveMax = findMax(numbers,count-1)
          if(recursiveMax>current){
              return recursiveMax;
          }

          return current;
        }
}


public static double findMax(double[] numbers, int count)
{
  double c = numbers[count-1];
  if (count==1)
    return c;
  else
  {
    double f = findMax(numbers, count-1);
    return c > f ? c : f;
  }
}


这篇关于递归问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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