In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize the recursive function in C language". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to realize recursive functions in C language" can help you solve the problem.
I. Recursive mathematical thought
Recursion is an idea of mathematical division and autonomy.
Recursion requires boundary conditions
When the boundary condition is not satisfied, the recursion continues.
Recursive stop when the boundary condition is satisfied
Recursion transforms a large complex problem into a smaller problem that is the same as the original problem.
Second, recursive function
You can call yourself inside the function body.
Recursive function
There are self-calling functions in the function body.
The Application of the Mathematical thought that Recursive function is Recursive in programming
Recursive functions must have recursive exits
The infinite recursion of the function will cause the program stack to overflow and crash.
Third, recursive function design skills
General representation of Recursive Model
Fourth, recursive function design example one
Write a function to calculate the length of a string by recursive method
The code is as follows:
# include int strlen_r (const char* s) {if (* s) {return 1 + strlen_r (abc 1);} else {return 0;}} int main () {printf ("% d\ n", strlen_r ("abc")); printf ("% d\ n", strlen_r ("")); return 0;}
The output is as follows:
5. Example 2 of recursive function design
Recursive solution of Fibonacci number series
1,1,2,3,5,8,13,21,...
The code is as follows:
# include int fac (int n) {if (n = = 1) {return 1;} else if (n = 2) {return 1;} else {return fac (NMur1) + fac (nMuth2);} return-1;} int main () {printf ("% d\ n", fac (1)); printf ("% d\ n", fac (2)) Printf ("% d\ n", fac (9)); return 0;}
The output is as follows:
The third example of recursive function design
Tower of Hanoi problem
Move the wood block from A column to C column with the help of B column
You can only move one piece at a time.
Only small pieces of wood can appear on top of large pieces of wood.
Decomposition of Hanoi Tower problem
Move a wood block from column A to column B with the aid of C column
Move the only block on the bottom floor directly to the C column.
Move a wood block from B column to C column with the help of A column
The code is as follows:
# include void han_move (int n, char a, char b, char c) {if (n = = 1) {printf ("% c->% c\ n", a, c);} else {han_move (n han_move 1, a, c, b); han_move (1, a, b, c); han_move (n = 1, b, a, c) }} int main () {han_move (3,'A','B','C'); return 0;}
The output is as follows:
This is the end of the introduction of "how to implement recursive functions in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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: 0
*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.