The example of this article analyzes the principle of Java. Share it for everyone for your reference. The specific analysis is as follows:
Explanation: Program calling its own programming skills is called recursion.
Program calling its own programming skills is called recursion. Recursive as an algorithm is widely used in programming languages. One process or function has a method that directly or indirectly calls itself in its definition or instructions. It usually transforms a large -scale complex problem into a problem of smaller scale similar to the original problem to solve. A small amount of procedures can be described to describe the multiple repeated calculations required for the problem solving process, which greatly reduces the code of the program. The ability to recurse is to define the unlimited collection of objects with limited sentences.
Three conditions of recursion:
Border condition recursive advanced segments Back to the return segment When the boundary conditions are not met, recursive forward; when the boundary conditions are met, recursive return.
The following two sample programs are explained:
Use the Java code to find the order 5. (5 steps of 5*4*3*2*1)
/***Calculate 5 (Result = 5*4*3*2*1)*@Author Pplsunny****/Public Class Test01 {Public Static void Main (String [] ARGS) {System.out.prin TLN (TLN ( f (5));} Public Static int F (int n) {if (1 == n) return 1; else return n*(n-1);}}
In this question, analyze according to the three conditions of recursive:
(1) Boundary conditions: step multiplier, multiplied to the last number, that is, when 1, return 1, the program execution to the end;
(2) Recursive front section: When the current parameters are not equal to 1, continue to call yourself;
(3) Recursive return section: start from the maximum number, if the current parameter is 5, then 5*4, 5*(5-1), that is, n*(n-1)
Use Java code to find the number: 1, 1, 2, 3, 5, 8 ...... 40th number
/ ** * Seek number: 1,1,2,3,5,8 ... the 40th number * @Author Pplsunny * */ Public Class Test_fibonacci {Public Static Void Main (String [] ARGS) {System.out.println (f (f (6));} Public Static int F (int n) {if (1 == n || 2 == n) Return 1; Else Return f (n-) + f ( n-2);}}
The breakthrough of this question is: starting from the 3rd digit, the number is the sum of the first two digits. To calculate the value of the number, then you need to calculate the number as a parameter.
(1) First of all, when the number of digits is 1 and 2, the current value returned should be 1;
(2) Then, when the number of digits is 3, the return value should be = 2 = 1+1;
When the number is 4, the return value = 3 = 2+1;
When the digit is 5, the return value = 5 = 3+2;
When the number is 6, the return value = 8 = 5+3;
... ...
(3) From (2), when it is learned that when it is more than or equal to 3, the value of the current digital (n) = f (n-1)+f (n-2)
Experience: Some beginners may think that recursion is to call themselves, wouldn't it be a dead cycle. Yes, if the recursive writing is unreasonable, it is the cycle of death. However, if the writing is reasonable, and the "boundary condition", when the program is executed to the end, it will return layer by layer. Just like we climb the mountain, we climb on the mountain road to climb layer after layer. Without the top of the mountain, we will always climb up. But if you reach the top of the mountain, climb down the steps when you go up the mountain.
It is hoped that this article is helpful to everyone's Java program design.