-
Many beginners tend to be confused about recursion and spend a lot of time on it. In fact, the example in the textbook is very classic, but it says a little nagging. Beginners will look at the big head.
Programming is problem-solving, and in reality many problems are relatively simple, not as complex as the Tower of Hanoi. We don't have to ask how recursion is implemented, we just have to be able to use recursion, and we can use recursion to solve some problems for us, and that's it.
Let's start with an example:
There is a febonacci sequence:
The problem is: find the nth number in this sequence.
Since its function prototype is: f(n)=f(n-1)+f(n-2), which can be easily written with recursion, and it is not troublesome at all:
int febc(int n)
It's simple, there's not much to say. But can you write the corresponding recursive function?
-
If you talk too much, you don't know what it is.
Question: It's better not to copy-paste...
Recursion is the phenomenon of the call itself appearing in a function, as in the simplest example, find factorial:
When n=0 or 1, n! =1ï¼›When n>1, n! =n*(n-1)!By such thoughts, the program is written as:
int fun(int n)
if(n<2)
return 1;
elsereturn n*fun(n-1);
Seeing that the fun function calls its own fun, it is conceivable that you can get the calculation result step by step.
-
Why do the two upstairs have to talk so complicated!
A recursive function is a function that calls itself (only with different arguments when it is called).
-
Recursion refers to the phenomenon of re-entrancy caused by a function or procedure or subroutine that directly or indirectly calls itself in a running program.
In computer programming, recursion refers to the process by which a function keeps referencing itself until the referenced object is known.
Use recursion to solve problems, think clearly, and less. However, in mainstream high-level languages (using recursive algorithms consumes more stack space, so should be avoided when stack size is limited. All recursive algorithms can be rewritten as non-recursive equivalents.
-
A program that invokes its own programming technique is called recursion. Recursion is widely used as an algorithm in programming languages.
A process or function in its definition or description has a method that directly or indirectly calls itself, it usually transforms a large and complex problem into a small problem similar to the original problem to solve, and the recursive strategy only needs a small number of programs to describe the multiple repeated calculations required in the solution process, which greatly reduces the amount of programs.
The power of recursion lies in defining an infinite set of objects in finite statements. In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. When the boundary conditions are not satisfied, recursively advance; When the boundary conditions are satisfied, the rolling back is returned.
Disadvantages of recursion: reed nuclei
The recursive algorithm solves the problem compared with the commonly used algorithms such as ordinary loops, and the operation efficiency is low. Therefore, recursion should be avoided unless there is no better algorithm or in a particular case where recursion is more appropriate. In the process of recursive calls, the system opens up a stack for each layer of return points, local quantities, etc. to store.
Too many recursions can easily cause stack overflows, etc.
The above content reference: Encyclopedia - Recursion.
-
Both recursion and iteration are types of loops.
To put it simply, recursion is the repeated call of the function itself to implement the loop. The difference between iteration and ordinary loop is that the variable participating in the operation in the loop is also the variable that saves the result, and the current saved result is used as the initial value of the next loop calculation.
In a recursive loop, when the termination condition is met, it returns layer by layer to end. The iteration uses a counter to end the loop. Of course, in many cases, it is a multi-cycle mixture, depending on the specific needs.
An example of recursion, for example, given an array of integers, uses a halved query to return the index of the specified value in the array, assuming that the array is sorted, and for the sake of description, assuming that the elements are all positive and the length of the array is an integer multiple of 2.
A half-split query is a type of query that is much faster than iterating through all the elements.
int find(int *ary,int index,int len,int value)
if(len==1) last element.
if (ary[index]==value)return index;A successful query returns an index.
return -1;Failed, returns -1
If the length is greater than 1, perform a half-fold recursive query.
int half=len/2;
Check whether the checked value is greater than the last value in the upper half, and if so, recursively query the second half.
if(value>ary[index+half-1])
return find(ary,index+half,half,value);
Otherwise, recursively query the top half.
return find(ary,index,half,value);
The classic example of iteration is the accumulation of real numbers, such as the sum of all real numbers from 1 to 100.
int v=1;
for(i=2;i<=100;i++)
v=v+i;
-
1. The main method solves the recursive formula.
A formula that solves for most recursive expressions. Give the recursive formula: t(n) =a * t(n b) +f(n) where a>=1, b>1, f(n) is the given function, and t(n) is the recursive expression defined on a non-negative integer.
2. Recursive tree solving.
We can use the recursive tree to guess the upper bound of the solution, and then use the substitution method to prove the correctness of the solution. The accuracy of solving a recursive tree depends on the accuracy with which the recursive tree is drawn.
3. Substitution method.
For example, if we solve the recursive t(n) = 2t(n 2) + n, we guess that the solution is o(nlgn), and we find a constant c such that t(n)<=cnlgn.
i.e., t(n) <2c(n 2)lg(n 2)+n <=cnlgn-cnlg2+n = cnlgn-cn+n
As long as c>=1 and t(n)<=cnlgn, our guess is correct.
It should be noted that the substitution method is entirely empirical, and usually the upper bound is determined by a recursive tree, and then the substitution method is used to prove it.
Recursion, that is, calling oneself in the process of running. The conditions that constitute recursion:1 >>>More
It's this function that makes sense within this defined domain.
Definition of the original function.
primitive function The known function f(x) is a function defined in an interval where there is a function f(x) such that there is any point in the interval. >>>More
What does decibel mean for sound?
What is the definition of an ellipse?