------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
First, the recursive function, the popular saying is that the function itself calls itself ...
such as: N!=n (n-1)!
You define the function f (n) =nf (n-1)
and f (n-1) is the function of this definition. This is recursion.
definition: The programming technique called recursion (recursion) of the program call itself. 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. Second, Why use recursion: The purpose of recursion is to simplify the programming, make the program easy to read
Third, the drawback of recursion:
Although non-recursive functions are highly efficient, they are difficult to program and less readable. The disadvantage of recursive function is that it increases the overhead of the system, that is to say, the stack memory takes up a little more each time recursively.
Iv. Three conditions for recursion: 1, boundary conditions2, recursive forward segment3, recursive return segmentRecursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
V. The difference between recursion and circulation
1, recursion:
<span style= "Background-color:rgb (255, 252, 246); ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> Advantages: The code is concise, clear, and easy to verify correctness. <span style= "White-space:pre" ></span> disadvantage: It needs a lot of function calls to run, if the number of calls is deep, each time you want to create a new variable, you need to add additional stack processing, will have a certain impact on the efficiency of the execution <span style= "White-space:pre" ></span>, consumes too much memory resources. 2, Cycle: <span style= "White-space:pre" ></span> Advantages: fast speed, simple structure. <span style= "White-space:pre" ></span> Cons: does not solve all the problems. Some problems are appropriate for using recursion instead of loops. If using loops is not difficult, it is best to use loops. </span></span>
Six, example demonstration
1. Use recursion to calculate the factorial of N:
Analysis: n!=n* (n-1) * (n-2) ... *
public int Dreturn (int n) {
if (n==1) {
return 1;
}else{
Return N*dreturn (n-1);
}
}
2. Use the recursive function to calculate the sum of 1 to N: 1+2+3+4+. +n
public int Dreturn (int n) {
if (n==1) {
return 1;
}else{
Return N+dreturn (n-1);
}
}
3. Require output a sequence: 1,1,2,3,5,8,11 ... (the sum of the first two digits of each number is required to use a recursive function)
A function is represented by the return of Java: F (n) =f (n-1) +f (n-2); F (0) = 1; F (1) = 1;
Analysis: x1=1; X2=1; x3=x1+x2; x4=x2+x3; ... ; Xn=x (n-1) +x (n-2)
public int F (int n) {
if (n==1) {
return 1;
}else if (n==2) {
return 1;
}else{
Return F (n-1) +f (n-2);
}
}
4.java reverse printing of individual elements in an integer array using a recursive method
public static void printall (int index,int[] arr) {
System.out.println (Arr[index]);
if (Index > 0) {
Printall (--index,arr);
}
}
public static void Main (string[] args) {
Int[] arr={1,2,3,4,5};
Printall (Arr.lenth-1,arr);
}
5. Programming Solution: If a heifer, starting from the birth of the fourth year, the annual birth of a cow, according to the law, how many cows in the first n years?
public static int cattle (int n) {
if (n<=0) {
return 0;
}else if (n<=3) {
return 1;
}else{
Return cattle (n-1) +cattle (n-3);
}
}
public static void Main (string[] args) {
int n=10;
System.out.println (n+ "+cattle (n) +" cattle ");
}
Dark Horse programmer----Java Basics---Recursive overview, recursion, and looping differences