In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "PHP recursive function algorithm and application". In daily operation, I believe many people have doubts about PHP recursive function algorithm and application problems. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts of "PHP recursive function algorithm and application"! Next, please follow the small series to learn together!
1. Meaning of calling subroutine:
When the main program reaches the statement calling subroutine A, the system saves some necessary field data, and then executes a GOTO statement similar to BASIC to jump to subroutine A (for simplicity, I ignore parameter passing here). When subroutine A executes to the statement calling subroutine B, the system does the same as above and jumps to subroutine B. After the subroutine B executes all the statements, jump back to the next statement of the subroutine B statement called by the subroutine A (I ignore the return value processing here). After the subroutine A executes, jump back to the next statement of the subroutine A statement called by the main program, and the main program executes to the end. To make a comparison: I am eating (execution of the main program) halfway, someone called me (execution of subroutine A), said halfway, the phone rang again (execution of subroutine B), I just need to answer the phone first, and then someone finished talking, *** finished eating (I am tired enough to eat this meal J).
2. Recognize recursive functions
We all learned mathematical induction in high school, PHP recursive algorithms such as:
Please N! We can get n! That definition means that requirement 3!, We have to find 2!, Request 2!, You have to ask for 1!, Request 1!, You have to ask for 0 first! and 0!= 1, so 1!= 0!* 1=1, and then 2!, 3!。Expressed separately as functions, we can observe that, except for computing 0! Except for the subroutine, the other subroutines are basically similar. We can design such a subroutine:
int factorial(int i){ int res; res=factorial(I-1)*i; return res; }
Then when executing the main program statement s=factorial(3), factorial(3) will be executed, but when executing factorial(3), factorial(2) will be called again. At this time, everyone should note that factorial(3) and factorial(2) are the same code segment, but their data area in memory is two! Factorial (1) is called when factorial(2) is executed, and factorial (0) is called when factorial (1) is executed. Every time factorial is called, it adds a new data area in memory. So these copied functions can be understood as multiple functions with different names; but we have a problem with this function. When factorial (0) is executed, it will call factorial (-1) again. To create an infinite loop, that is, in the factorial function, we must ensure that the function is not called again at the appropriate time, that is, we do not execute res=factorial(I-1)*i;. So the function changes to:
int factorial(int i){ int res; if (I>0) res=factorial(I-1)*i; else res=1; return res; }
3. How to consider using PHP recursive algorithm to solve problems
For example: find s=1+2+3+4+5+6+…+n originally this problem we used to use the method of cyclic accumulation. If you want to use the recursive method, you must consider two points:
1) whether the problem can be transformed into a recursive form of description;
2) Whether there are boundary conditions for recursion to end.
Obviously, both conditions for recursion apply:
1) s(n) =s(n-1)+n 2) s(1)=1
So the source code is:
int progression(int n){ int res; if (n=1 )res=1 else res=progression(n-1)+n; return res; }
4. Application of recursion
in-order traversal binary tree
void inorder (BinTree T){ if (T){ inorder(T->lchild); printf("%c",T->data); inorder(T->rchild); } } At this point, the study of "PHP recursive function algorithms and applications" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 209
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.