Java递归理解

时间:2013-11-01 06:14:05

标签: java recursion

有人可以帮助我理解这个解决方案,并且通过帮助我的意思是帮助我迭代所有事情,所以有人像我能理解的那样愚蠢。非常感谢你:(

         public void recurse(int n, char one, char two, char three)
         {
               if(n == 1)
               System.out.println(n + " " + one + " " + two);
               else
               { 
                   recurse(n - 1, one, three, two);
                   System.out.println(n + " " + one + " " + two);
                }
          }

函数调用:

          recurse (4, 'A', 'B', 'C');

5 个答案:

答案 0 :(得分:1)

this.recurse (4, 'A', 'B', 'C');

第一轮: n不是== 1 - > recurse(3, 'A', 'C', 'B');

第二轮: n不是== 1 - > recurse(2, 'A', 'B', 'C');

第三轮: n不是== 1 - > recurse(1, 'A', 'C', 'B');

第四轮: n == 1 - >控制台打印:“1 A C”

重新启动递归链:

控制台打印:“2 A B”

控制台打印:“3 A C”

控制台打印:“4 A B”

控制台中的最终输出将是

1 A C
2 A B
3 A C
4 A B

答案 1 :(得分:1)

每次从该行递归调用该方法时它都会切换 - 递归调用多少次?请记住,每次调用该方法时,参数都不同于任何其他调用。 - user201535

这位用户对我说得很清楚,再次感谢你。

答案 2 :(得分:1)

从另一个函数调用一个函数的想法立即表明函数调用自身的可能性。 Java中的函数调用机制支持这种可能性,称为递归。递归是一种功能强大的通用编程技术,是许多至关重要的计算应用的关键,从组合搜索和排序方法方法,提供信息处理的基本支持(第4章)到信号处理的快速傅里叶变换(第9)。

你的第一个递归程序。 递归的HelloWorld是实现阶乘函数,它通过等式为正整数N定义

N! = N × (N-1) × (N-2) × ... × 2 × 1 

N!使用for循环很容易计算,但Factorial.java中更简单的方法是使用以下递归函数:

public static int factorial(int N) { 
   if (N == 1) return 1; 
   return N * factorial(N-1); 
} 

你可以通过注意factorial()返回1 = 1来说服自己产生所需的结果!当N为1时,如果它正确计算值

(N-1)! = (N-1) × (N-2) × ... × 2 × 1 

然后它正确计算值

N! = N × (N-1)! = N × (N-1) × (N-2) × ... × 2 × 1 

我们可以跟踪跟踪任何函数调用序列的方式跟踪此计算。

factorial(5) 
   factorial(4) 
      factorial(3) 
         factorial(2) 
            factorial(1) 
               return 1 
            return 2*1 = 2 
         return 3*2 = 6 
      return 4*6 = 24 
   return 5*24 = 120

我们的factorial()实现展示了每个递归函数所需的两个主要组件。

The base case returns a value without making any subsequent recursive calls. It does this for one or more special input values for which the function can be evaluated without recursion. For factorial(), the base case is N = 1.

The reduction step is the central part of a recursive function. It relates the function at one (or more) inputs to the function evaluated at one (or more) other inputs. Furthermore, the sequence of parameter values must converge to the base case. For factorial(), the reduction step is N * factorial(N-1) and N decreases by one for each call, so the sequence of parameter values converges to the base case of N = 1. 


Now in your case (n==1) is the base condition or terminating condition.
recurse(4,'A','B','C')
  recurse(3,'A','C','B')
     recurse(2,'A','B','C')
        recurse(1,'A','C','B')

        return : 1 A C
     return : 2 A B
   return : 3 A C
return : 4 A B

final output : 
1 A C
2 A B
3 A C
4 A B

答案 3 :(得分:0)

首先,您需要了解什么是递归。

递归意味着一个方法一次又一次地调用自己,所以现在你会想知道函数将一直调用自己,所以当它停止时? 这是基本条件

即在您的代码中是(n == 1)

当n == 1时,该函数将停止调用自身并打印结果。

您使用值 4

调用该函数

所以第一次通话将是递归(4,'A','B','C');

它将进入函数并在第一次迭代中查看是否n == 1 4

它将再次使用 n-1 调用该函数,该函数将 3

它会继续调用这个函数,直到n等于1。

然后会打印 1 A B

希望你现在明白。

答案 4 :(得分:0)

首先,它将转到最后一行,     递归(4,'A','B','C'); 这是一个名为'recurse'的函数调用,它在参数中传递,对应于(int n,char one,char two,char 3);

现在调用函数时,传入params, 它检查(n == 1)的值,如果这个条件有效,它通过这个语句写'n','one','two'的值     System.out.println(n +“”+ one +“”+ two);

然后如果这个(n == 1)条件无效,它会跳转到     其他     {         递归(n - 1,一,三,二);         System.out.println(n +“”+ one +“”+ two);     } 现在这里是递归发生的地方,意味着函数'recurse'在自己的身体中一次又一次地调用自己。

现在当它进入其他人的身体时 - 它会,所以它会调用递归(n - 1,一,三,二);并且函数recurse将再次执行,这将发生直到n = 1,即 - 在第4次迭代中。并且功能完成。