This article summarizes the C language recursive operation usage. Share to everyone for your reference, specific as follows:
Using inductive method to understand recursion
Stepping Expression : An expression that transforms a problem into a child problem
End Condition : When can no longer be a stepping expression
Direct Solution Expression : An expression that can directly evaluate a return value at the end condition
Logical Inductive Entry : Applies to all the handling of a child problem that is not applicable to an ending condition, and of course the above step expression is actually included in this.
General form of recursive algorithm:
void func (Mode)
{
if (endcondition)
{
constexpression //Base item
}
else
{
accumrateexpreesion //Inductive entry
mode=expression //Stepper Expression
func (mode) //call itself, recursive
}
}
The most typical is the n! algorithm, which is the most persuasive. Understand the idea of recursion and the use of the scene, the basic can be designed, of course, and other algorithms to combine the use of, but also need to continue to practice and summary.
#include "stdio.h"
#include "math.h"
int main (void)
{
int n, RS;
printf ("Enter the number of n that you want to calculate factorial:");
scanf ("%d", &n);
rs = factorial (n);
printf ("%d", RS);
}
Recursive computation procedure
int factorial (n) {
if (n = = 1) {return
1;
}
return n * factorial (n-1);
}
The basic idea of recursion is to transform the large scale problem into small scale similar child problem to solve. When the function is implemented, because the method of solving the big problem and the method of solving the small problem is often the same method, it produces the case that the function calls itself. In addition, the problem-solving function must have a clear end condition so that no infinite recursion occurs.
Problems that can be solved by recursion must meet two conditions:
① can reduce the scale of the problem by recursive invocation, and the new problem has the same form as the original problem .
there is a simple situation in ② that allows recursion to exit in a simple context .
If a problem does not meet the above two conditions, then it can not be resolved by recursion.
For ease of understanding, take the Fibonacci sequence: The value of the nth item in the Fibonacci sequence.
This is a classic question, and when it comes to recursion, it must be mentioned. The Fibonacci sequence is defined as: F (0) = 0, f (1) = 1, n > 1, f (n) = f (n-1) + f (n-2)
This is an obvious problem that can be solved recursively. Let's take a look at how it satisfies the recursive two conditions:
1. For a n>2, f (n) only needs F (n-1) and F (n-2), i.e. the problem of scale N, which translates into smaller problems;
2. For n=0 and N=1, there is a simple situation: f (0) = 0, f (1) = 1.
Therefore, it is easy to write a recursive program that calculates the nth term of the Fibonacci sequence:
int fib (n) {
if (n = 0) return
0;
else if (n = = 1) return
1;
else return
F (n-1) + f (n-2);
}
When writing a recursive function, it is important to write the judgment of the simple situation at the front, so that the function call can abort the recursion in a timely manner when checking the simple situation , otherwise, your function may be recursively invoked there forever.
Judge whether a string is a palindrome:
function Huiwen ($str)
{
if (strlen ($str) ==1 | | strlen ($STR) ==0) {return
1;
} else{
if ($str [0]== $str [Strlen ($STR)-1]) {
$str = substr ($str, 1,-1);;
echo $str. " <br/> ";
Return Huiwen ($STR);
else{return
0;}}
I hope this article will help you with the C language program design.