Java recursion Understanding [closed]

我的梦境 提交于 2019-12-25 02:40:47

问题


Can someone please help me understand this solution, and by help I mean help me iterate through everything so someone as stupid as I can understand it. Thank you so much :(

         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);
                }
          }

function call:

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

回答1:


It switches every time the method is called recursive from that line - how many times is it called recursively? Remember that each time the method is called the parameters are distinct from any other invocation. – user201535

This user made it pretty clear for me, thank you once again.




回答2:


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

1st round: n is not ==1 --> recurse(3, 'A', 'C', 'B');

2nd round: n is not ==1 --> recurse(2, 'A', 'B', 'C');

3rd round: n is not == 1 --> recurse(1, 'A', 'C', 'B');

4th round: n ==1 --> console prints: "1 A C"

Coming back up the recursion chain:

console prints: "2 A B"

console prints: "3 A C"

console prints: "4 A B"

Final output in console would be

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



回答3:


The idea of calling one function from another immediately suggests the possibility of a function calling itself. The function-call mechanism in Java supports this possibility, which is known as recursion. Recursion is a powerful general-purpose programming technique, and is the key to numerous critically important computational applications, ranging from combinatorial search and sorting methods methods that provide basic support for information processing (Chapter 4) to the Fast Fourier Transform for signal processing (Chapter 9).

Your first recursive program. The HelloWorld for recursion is to implement the factorial function, which is defined for positive integers N by the equation

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

N! is easy to compute with a for loop, but an even easier method in Factorial.java is to use the following recursive function:

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

You can persuade yourself that it produces the desired result by noting that factorial() returns 1 = 1! when N is 1 and that if it properly computes the value

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

then it properly computes the value

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

We can trace this computation in the same way that we trace any sequence of function calls.

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

Our factorial() implementation exhibits the two main components that are required for every recursive function.

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



回答4:


First you need to understand what is recursion.

Recursion means a method calling itself again and again so now you will wonder the function will always be calling itself so when it will stop? Here comes the base condition

i.e in your code is if (n == 1)

when n==1 the function will stop calling itself and print the result.

you call the function with the value 4

so first call will be recurse (4, 'A', 'B', 'C');

it will go into the function and see whether n==1 which is 4 in first iteration.

it will again call the function with n-1 which will be 3

It will continue calling the function like that till the n becomes equal to 1.

and then it will print 1 A B

Hope you get it now.




回答5:


First, it will go to the last line, recurse (4, 'A', 'B', 'C'); this is function named 'recurse' calling, its passing in the parameters, corresponding to the (int n, char one, char two, char three);

now when the function is called, and passed in params, it checks the value of (n == 1), if this conditions stands valid, it writes the value of 'n', 'one', 'two' through this statement System.out.println(n + " " + one + " " + two);

then if this (n == 1) condition is not valid, it jumps to the else { recurse(n - 1, one, three, two); System.out.println(n + " " + one + " " + two); } now here is where the recursion is happening, means a function 'recurse' is calling itself again and again in its own body.

now when it enters the else's body - and it will, so it will call recurse(n - 1, one, three, two); and the function recurse will execute again, this will happen till the n = 1, that is - in the 4th iteration. and the function is completes.



来源:https://stackoverflow.com/questions/19721747/java-recursion-understanding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!