【问题标题】:Java summing recursive methodJava求和递归方法
【发布时间】:2013-10-21 14:33:19
【问题描述】:

给定一个整数数组,并尝试定义递归方法 sum(int[] A,int s, int e) 来计算数组 A 的总和,其中 s 和 e 是起始索引和结束索引。我想用给定的数组 int[] A= {3,5,8,9,10} 对其进行测试。

我对如何做到这一点感到困惑,但这是我到目前为止所拥有的(我什至对这里的代码有点困惑,因为我的伙伴帮助我编写了它,稍微解释一下会有很大帮助!):

public static int sum(int[]A,int s,int e) {
   if (s==e)
      return A[e];
else
   return A[5] + sum(A,s+1,e);

【问题讨论】:

  • 为什么在这样的算法中使用递归调用?这是不好的方法。
  • 这个方法怎么称呼?
  • 最后一行有错;它应该是return A[s] ... 而不是return A[5] ...。除此之外,你到底对什么感到困惑?
  • @StanislavMamontov,答案几乎可以肯定是“学习递归”。
  • @pamphlet 人们在递归方面犯的最大错误之一就是在完全没有必要时使用它。导师或任何教他们的人应该使用适合递归的示例,这样他们就可以看到为什么它是如此有用,因为它是上下文。

标签: java arrays recursion


【解决方案1】:

正如@KlasLindbäck 的回答中所发布的,5 应该是 s。

public static int sum(int[]A,int s,int e) {
   if (s==e)
      return A[e];
else
   return A[s] + sum(A,s+1,e);

提供解释:

首先,调用这个方法:

int theSum = sum(myArray, 0, myArray.length-1);

我会为你运行 {3,5,8,9,10} 数组。

sum(A, 0, 4):
return A[0] + sum(A, 1, 4)   //3 + sum(A, 1, 4)

sum(A, 1, 4):
return A[1] + sum(A, 2, 4)   //5 + sum(A, 2, 4)

sum(A, 2, 4):
return A[2] + sum(A, 3, 4)   //8 + sum(A, 3, 4)

sum(A, 3, 4):
return A[3] + sum(A, 4, 4)   //9 + sum(A, 4, 4)

sum(A, 4, 4):
return A[4]                  //10

Now, we know that sum(A, 4, 4) is 10, so therefore sum(A, 3, 4) is 9 + 10 = 19.
Now, we know that sum(A, 3, 4) is 19, so therefore sum(A, 2, 4) is 8 + 19 = 27.
Now, we know that sum(A, 2, 4) is 27, so therefore sum(A, 1, 4) is 5 + 27 = 32.
Now, we know that sum(A, 1, 4) is 32, so therefore sum(A, 0, 4) is 3 + 32 = 35.

【讨论】:

    【解决方案2】:

    您误读了一个字符。 5 应该是返回行上的 s

    return A[s] + sum(A,s+1,e);
    

    【讨论】:

      【解决方案3】:
        package com.TTC.Tryfon.AbdulRahman;
      
      public class Doing {
          public static void main(String[] args) {
              int [] z={3,5,8,9,10};
              System.out.println(sum(z,0));   
          }
      
          public static int sum(int [] a,int index){
              if(index<a.length){
                  return a[index]+sum(a,index+1); 
              }
              return 0;   
          }
      
      }
      

      上面的程序会产生如下结果:

      35
      

      为了理解程序让我们执行这部分:

      if(index<a.length){
          return a[index]+sum(a,index+1); 
      }
      

      索引从 0 开始,并且 a.length=5:

      if(0<5){
          return a[0]+sum(a,0+1);
          // this means return 3+sum(a,1);    
      }
      
      if(1<5){
          return a[1]+sum(a,1+1);
          // this means return 5+sum(a,2);    
      }
      
      if(2<5){
          return a[2]+sum(a,2+1);
          // this means return 8+sum(a,3);    
      }
      if(3<5){
          return a[3]+sum(a,3+1);
          // this means return 9+sum(a,4);    
      }
      if(4<5){
          return a[4]+sum(a,4+1);
          // this means return 10+sum(a,5);   
      }
      if(5<5){
          // 5 is not smaller than 5, so it will return 0;
          }
      return 0;
      

      由于没有更多的函数调用,我们必须将返回的数字替换为函数调用:

      10+0
      9+10
      8+18
      5+27
      3+32 =35
      

      这是我第一次解释,我希望它是好的。

      【讨论】:

        【解决方案4】:

        您还可以在递归调用中包含停止条件:

        public static int sum(int[]A,int s,int e) {
             return A[s] + (s == e) ? 0 : sum(A, s+1, e);
        }
        

        【讨论】:

        • 当然可以。但是说“你也可以这样做”并在没有解释它是什么以及它是如何工作的情况下加入他们可能从未见过的运算符,当他们说他们并不真正理解在更具可读性的版本。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        相关资源
        最近更新 更多