Description of the problem: what is recursion? What is the use of? and use Java to implement a simple recursive program?
1. What is recursion?
Recursion (recursion) is widely used as an algorithm in programming language, which refers to the re-entry phenomenon that the function calls itself directly or indirectly in the process of running.
from the Baidu Encyclopedia explanation:
Recursion as an algorithm is widely used in programming language. A procedure or function has a method of directly or indirectly invoking itself in its definition or description, which usually transforms a large and complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program. The ability to recursion is to define an infinite set of objects with limited statements. in general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
2. What are the conditions that make up a recursive requirement?
(1) The sub-problem must be the same as the original problem, and more simple; (2) can not be unlimited to call itself, there must be an export, degenerate to non-recursive state processing.
3. Recursive Application!!!
Recursive algorithms are generally used to solve three types of problems:
(1) The definition of the data is defined by recursion.
For example: Fibonacci (Fibonacci) function, F[n]=f[n-1]+f[n-2] (where n>1,f[0]=0,f[1]=1)
(2) The problem solution is implemented by recursive algorithm.
Although there is no obvious recursive structure in this kind of problem, it is simpler to solve it by recursion than iterative solution, such as Hanoi problem.
(3) The structural form of the data is defined by recursion.
such as binary tree, generalized table, etc., due to the inherent recursive nature of the structure, their operations can be recursively described.
4. Disadvantages of recursion
Recursive algorithm is a relatively common algorithm for solving problems, such as ordinary cycle, the operation efficiency is low.
Therefore, recursion should be avoided as much as possible, unless there is no better algorithm or a specific situation where recursion is more appropriate.
Because in the process of recursive invocation, the system opens up stacks for each layer's return point, local quantity, etc. Too many recursion times can cause stack overflow and so on.
5. How do I design a recursive algorithm?
(1) Determining the recursive formula
(2) Determining the boundary (end) conditions
6. Examples of recursive applications
(1) Calculation n! Algorithm for (n factorial factorial)
public class Factorial {
public static void Main (string[] args) {
System.out.println ("Please enter an integer greater than or equal to 0");
Scanner scan=new Scanner (system.in);
int N=scan.nextint ();
if (n<0) {
SYSTEM.OUT.PRINTLN ("Invalid input value");
}else{
int result=dofactorial (n);
SYSTEM.OUT.PRINTLN (n + "factorial is:" + result);
}
}
Valid code
public static int dofactorial (int n) {
if (n==0 | | n==1) {
return 1;
}else{
return n*dofactorial (n-1);
}
}
}
(2) Implementation of Fibonacci functions
public class Fibonacci {
public static void Main (string[] args) {
System.out.println ("Please enter a positive integer:");
Scanner scan=new Scanner (system.in);
int N=scan.nextint ();
if (n<0) {
SYSTEM.OUT.PRINTLN ("Input value is illegal! ");
}else{
int result=dofibonacii (n);
System.out.println (n+ "Fibonacci value is:" + result);
}
}
public static int dofibonacii (int n) {
if (n==0 | | n==1) {
return n;
}else{
return Dofibonacii (n-1) +dofibonacii (n-2);
}
}
}
Java Foundation------Recursion (Java implementation)