Explanation: The programming technique of program calls itself is called recursion.
The programming technique of a program calling itself is called recursion. Recursion is widely used as an algorithm in programming languages. A process or function has a method to directly or indirectly call itself in its definition or description. It usually converts a large and complex problem layer by layer into a smaller problem similar to the original problem to solve. The recursive strategy only requires a small number of programs to describe the multiple repeated calculations required for the problem-solving process, greatly reducing the amount of code in the program. The ability of recursion lies in using finite statements to define an infinite set of objects.
Three conditions for recursion:
1. Boundary conditions
2. Recursive advancement section
3. Recursively return segment
When the boundary condition is not satisfied, proceed recursively; when the boundary condition is satisfied, return recursively.
The following are two example programs:
1. Use Java code to find the factorial of 5. (Factory of 5 = 5*4*3*2*1)
package org.wxp.recursion; /** * Calculate the factorial of 5(result = 5*4*3*2*1) * @author Championship.Wong */ public class Test01 { public static void main(String[] args) { System.out.println(f(5)); } public static int f(int n) { if (1 == n) return 1; else return n*(n-1); } } In this question, the three conditions of recursion are analyzed:
(1) Boundary conditions: factorial, multiply to the last number, that is, 1, return 1, and the program will be executed to the end;
(2) Recursive advancement section: When the current parameter is not equal to 1, continue to call yourself;
(3) Recursively return segment: start from the largest number, if the current parameter is 5, then it is 5*4, that is, 5*(5-1), that is, n*(n-1)
2. Use Java code to find the sequence: 1, 1, 2, 3, 5, 8......40th digit number
package org.wxp.recursion; /** * Find the sequence: 1, 1, 2, 3, 5, 8...... 40th digit number*/ public class Test_02_Fibonacci { public static void main(String[] args) { System.out.println(f(6)); } public static int f(int n ) { if (1== n || 2 == n) return 1; else return f(n-1) + f(n-2); } } 3. Problem description: Solve the value of the 10th position of the Fibonacci sequence? (Fibonacci Sequence, also known as the golden-divided sequence, refers to a sequence: 1, 1, 2, 3, 5, 8, 13, 21, ... In mathematics, the Fibonacci sequence is defined in the following recursive method: F0=0, F1=1, Fn=F(n-1)+F(n-2) (n>=2, n∈N*))
Program List:
/** *<p>Title: Java recursive algorithm example</p> *<p>Description: Use recursive algorithm to solve the value of the fifth number of the Fibonacci sequence</p> *<p>Filename:Fibonacci.java</p> */ public class Fibonacci { /** *Method description: Recursive algorithm to solve the Fibonacci sequence*Input parameters: int n *Return type: int */ public static int fun(int n) { if(1==n || 2==n) { return 1; } else { return (fun(n-1) + fun(n-2)); } } /** *Method description: main method*Input parameters: String[] args *Return type: void */ public static void main(String[] args) { System.out.println(fun(10)); } }The running results are as follows:
Copy the code as follows: 55