Recursion: A process (method) that invokes itself directly or indirectly, this process is called recursion.
For example such a small program:
Package demo;
public class Demo {
public static void Main (string[] args) {
Demo5 demo=new Demo5 ();
int Num=demo.test (3);//int num=demo.test (4); int num=demo.test (5);
SYSTEM.OUT.PRINTLN (num);
}
}
Class Demo5 {
public int test (int a) {
int result;
if (a==1) {
return 1;
}
Call the test method itself to implement recursion
Result=test (A-1) *a;
return result;
}
}
In this program, the test method inside the class Demo5 calls the test method itself, which is recursion. When running in the main method, when the argument is 3 o'clock, the output num result is 6, the parameter is 4 o'clock, the result is 24, the parameter is 5 o'clock, and the result is 120.
The process of these results is as follows:
1. When the argument is 3 o'clock, that is, int num=demo.test (3), this time will go into the test method, first pass if the judge a! =1, so go directly to the next step, that is, Result=test (3-1), that is, result=test (2);
Because here is the test method, the parameter is 2, so the test method will be called here again, the next step if the judge, is still a! =1, the result of test (2) is Test (1) * *;
Then in the previous step Result=test (2) in the test (2) will be replaced, the result is result=test (1) *2*3;
After this step, the program does not end, because the side still has the test method, then the test will be called again, if you judge, this time a==1, then return 1, that is, test (1) = 1;
Then, the result above will change again, namely: Num=result=test (1) *2*3=1*2*3=6. This is when the parameter is 3, the result is a recursive run of 6.
2. Similarly, when the parameter is 4, that is, int num=demo.test (4); First A! =1, then run to the next step is result=test (3);
Immediately after, Test (3) after the running step is the same as the parameter is 3, then the final result is: Num=result=test (3) *4=1*2*3*4=24;
3. Then, when the parameter is 5, follow the above step, which will be programmed at the very beginning of test (4), then the result is 24*5=120.
Again for example the following example of the summation and:
Package demo;
public class Test1 {
public static void Main (string[] args) {
Demo11 demo=new Demo11 ();
int Num=demo.test (3);//int num=demo.test (4); int Num=demo.test (3)
SYSTEM.OUT.PRINTLN (num);
}
}
Class demo11{
public int Test (int n) {
int sum;
if (n>0) {
Call the test method itself to implement recursion
Sum=n+test (n-1);
return sum;
}else{
return 0;
}
}
}
This example is for all positive integers that are smaller than the positive integers we specify, and the test method itself is called in the test method in the Demo11 class, where recursion is used.
In this case, the parameter is 3, the result is 6, the parameter is 4, the result is 10, the argument is 5, and the result is 15. The implementation process is as follows:
1. When the parameter is 3, that is n=3, first if judgment n>0, then Sum=3+test (2), and then test (2) again if the n>0, the result is 2+test (1); then the whole sum result becomes sum=3+test (2) = 3+2+test (1);
Then, continue to test (1), the result is 1+test (0), then continue test (0), N=0,return 0, the result is 0; then the result of the whole sum is sum=3+2+1+0=6.
2. When the parameter is 4, n=4, the same, the first is Sum=4+test (3), then the step of repeating test (3), the result is sum=4+test (3) =4+3+2+1+0=10.
3. When the parameter is 5, n=5, then the first if is sum=5+test (4); then sum=5+4+3+2+1+0=15.
We used to learn from 1 plus to 100 of the cumulative sum, the result is 5050, then here if the parameter is set to 100, the result is 5050, is exactly the same, and the calculation steps are the same as the top.
The recursive process can actually be seen as a constant call to replace the process, some people think the advantage of recursion is: the use of recursive algorithm than the use of iterative algorithm to be more clear and simple.
When writing recursion, you must use if the method is forced to return when the recursive call does not execute, or the program will never return.
Learning Diary (10) Java recursion