-
A function has arguments and a return value.
Look at a function:
function jiecheng (n:longint):longint;
beginif n=1 then jiecheng:=1
else jiecheng:=jiecheng(n-1)*n;
end;The function of this function is to calculate the factorial of n.
This function calls the function itself when it calculates the return value, which is recursion.
For example, find the factorial of 5:
At the beginning n=3
Then execute jiecheng:=jiecheng(3-1)*3
Then we start calculating jiecheng(3-1).
So the function is called again.
Starts calculating jiecheng(2-1)*2
Then the function is called again.
This time the parameter n=1 so the return value of jiecheng(2-1) is 1
Return to the function jiecheng:=jiecheng(2-1)*2 in the previous layer.
At this point, jiecheng(2-1) has a value, and it starts to return the value jiecheng:=2
This value is then returned to the function jiecheng of the first layer: =jiecheng(3-1)*3
jiecheng(3-1) participates in the computation due to the return value of the function at the next level, which is equivalent to 2*3
Finally, the value of jiecheng(3-1)*3 is 6, and then 6 is returned to the main program as the final return value.
In addition, actually.
jiecheng(3) is the factorial of 3.
jiecheng(3-1) is equivalent to the factorial of 2.
When you calculate the factorial of 3, you are actually finding 3*(2*1), which is 3*(the factorial of 2).
So when we calculate the factorial of three, we use 3*, which is the factorial of 3-1
How do you find the factorial of 3-1? Call the function again, of course.
The program then saves the current value, sets it aside, turns around and starts to calculate the 3-1 factorial.
Then take out the calculated value of the 3-1 factor, and then take out the equation that was put aside and perform the calculation.
This process is also done when calculating the factorial of 2.
So the recursive process is completed.
That's the idea of this factorial function.
-
Recursion is related to recursion.
-
Complete the following questions recursively.
1.Find the maximum number in the array.
3.Find the product of n integers.
4.Find the average of n integers.
5.Find the greatest common divisor and the least common multiple of n natural numbers.
6.There is a pair of male and female rabbits, and every two months they breed a pair of male and female rabbits. Q: How many pairs of rabbits are there after n months?
7.Known: Sequence 1, 1, 2, 4, 7, 13, 24, 44, ,..Find the nth term of the series.
8.Calculate xn. x,n is entered by the keyboard. Writing xn as a recursive function allows n to be positive, negative, and zero.
It's simple enough.
-
A binary tree consists of many nodes, all of which are of type node, with lchild pointing to its left subnode and rchild pointing to its right. They only point to nodes of the same type as themselves, not to themselves. It should be noted that bitree is only a type, and the variable of this type is a pointer to the variable of the node type, as for which specific node variable to point to, it still depends on the needs of the algorithm.
-
The bitree is the node of the binary tree, the type is node, lchild is the left child node, and rchild is the right child node, and both child nodes have their children at the same time (i.e. like their parent nodes). Like local variables in recursive functions, lchild and rchild can also be understood as "local variables in the binary tree definition", so they are not pointing to themselves.
-
Boundary conditions and recursive equations are two elements of a recursive function.
Recursive benefits: It is more concise and clear, readable, and easy to use mathematical induction to prove the correctness of the algorithm, so it brings great convenience to designing algorithms and debugging programs; In addition, it can be easily used to solve problems with some data structures that are originally defined recursively.
The fact that recursion is readable well may be objectionable to beginners. Actually, recursion is clearer, but from a learning perspective, it may not be easy to understand what really happens with recursion, how it is called, the call hierarchy and route, and what is stored in the call stack. But there's no denying that recursion is more concise.
Generally speaking, a person may be able to easily write a recursive algorithm with binary tree traversal in front, middle and back order, but it is more challenging to write a corresponding non-recursive algorithm, and I am afraid that at least half of the people will not be able to do it. So recursion is more concise and straightforward.
Disadvantage of recursion: Since recursion requires a system stack, the space consumption is much larger than that of non-recursive**. Also, if the recursion depth is too large, the system may not be able to hold up.
Recursion, that is, calling oneself in the process of running. The conditions that constitute recursion:1 >>>More
Love is one-on-one, but there is no limit to liking, for example, you like your parents, your siblings, friends, etc.; Like is also a kind of love, but not love; Love is the most difficult feeling between men and women, it can be produced in 1 second, you can give up your life for love, give up friendship, give up family affection, everyone's love values are different, believe in your own feelings, this is what you need most.
qdii**。
It refers to the establishment in the territory of a country and the approval of the relevant departments of the country to engage in overseas markets. >>>More
1. Different definitions:
MIC: Minimum inhibitory concentration, which is the lowest inhibitory concentration in the dilution method for microbial identification, which is the lowest inhibitory concentration of the drug that completely inhibits the growth of bacteria in a test tube or in a small well. >>>More
If you put it now, the friendship between the old wolf and Huang Lei is an iron-level relationship, and it is a very iron one. >>>More