"Learning Notes", "C language" recursion

Source: Internet
Author: User

First, the basic content:
Functions in the C language can be called recursively, that is, they can be directly (simply recursive) or indirectly (indirectly) self-tuning themselves.
Points:
1. The C language function can be called recursively.
2, can be called directly or indirectly two ways. Only direct recursive calls are currently discussed.

Ii. Conditions of recursion
The following three conditions must be met in order to solve the problem using a recursive method:
1, the problem to be solved can be transformed into a new problem, and the solution of this new problem is still the same as the original solution, but the object of the process is gradually increasing or decreasing.
Description: The method of solving the problem is the same, the parameters of the calling function are different each time (regular increment or decrement), if there is no rule can not be applied recursive call.
2. This conversion process can be applied to solve the problem.
Description: Using other methods is cumbersome or difficult to solve, and the use of recursive method can be a good solution to the problem.
3, must have a clear end of the conditions of recursion.
Note: Be sure to be able to end the recursive invocation where appropriate. This may cause the system to crash.

Third, the Code

1 /*2 A function is designed to calculate the n-th square of B3  4 2 conditions for recursion:5 1. function calls itself6 2. Must have a definite return value7 */8#include <stdio.h>9 intPow2 (intBintn);Ten  One intMain () A { -     intc = Pow2 (3,2); -      theprintf"%d\n", c); -     return 0; - } -  + /* - Pow2 (b, 0) = = 1 + Pow2 (b, 1) = = b = = Pow2 (b, 0) * b A Pow2 (b, 2) = = B*b = = Pow2 (b, 1) * b at Pow2 (b, 3) = = B*b*b = = Pow2 (b, 2) * b -   - 1> N is 0, and the result is definitely 1. - 2> N>0,pow2 (b, n) = = Pow2 (b, n-1) * b -  */ -  in intPow2 (intBintN) - { to     if(N <=0)return 1; +     returnPow2 (b, n1) *b; - } the  * /* $ int pow2 (int b, int n)Panax Notoginseng { - //Used to save calculation results the int result = 1; +      A      the //result *= B; + //result *= B; - //result *= B; $ //result *= B; $     //.... -      - //n Times the  -     Wuyi for (int i = 0; i<n; i++) the     { - result *= b; Wu     } -      About return result; $ }*/

"Learning Notes", "C language" recursion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.