Summary of the usage of recursive operations in C language

  • 2020-05-09 18:55:35
  • OfStack

This example summarizes the use of recursive operations in the C language. I will share it with you for your reference as follows:

To understand recursion by induction

Step expression: the expression in which the problem degenerates into a subproblem
End condition: when can I stop using step expressions
Direct solution expression: the expression that can directly evaluate the return value under the end condition
Logical induction term: applies to the processing of subproblems that do not apply to the closing condition. Of course, the above step expression is actually included in this.

The form of recursive algorithm 1:


void func( mode)
{
 if(endCondition)
 {
  constExpression   // The basic items 
 }
 else
 {
  accumrateExpreesion  // Inductive item 
  mode=expression   // Step expression 
   func(mode)   // Call itself, recursion 
 }
}

The most typical is N! Algorithm, this is the most convincing. Once you understand the idea of recursion and the usage scenarios, you can basically design your own algorithm. Of course, if you want to combine it with other algorithms, you still need to practice and summarize it.


#include "stdio.h"
#include "math.h"
int main(void)
{
 int n, rs;
 printf(" Please enter the number to calculate the factorial n : ");
 scanf("%d",&n);
 rs = factorial(n);
 printf("%d ", rs);
}
//  Recursive computation 
int factorial(n){
  if(n == 1) {
   return 1;
  }
  return n * factorial(n-1);
}

The basic idea of recursion is to turn large problems into small, similar subproblems. When a function is implemented, because the method to solve a big problem and the method to solve a small problem are often the same method, the function calls itself. In addition, the function that solves the problem must have an obvious closing condition, so that there is no infinite recursion.

A problem that can be solved recursively must satisfy two conditions:

The size of the problem can be reduced by recursive calls, and the new problem has the same form as the original problem.
There is a simple situation, you can make the recurrence exit in the simple situation.

If a problem does not satisfy the above two conditions, then it cannot be solved recursively.

For the sake of convenience, let's take the Fibonacci sequence: find the value of the N term in the Fibonacci sequence.

This is a classic problem, and when we talk about recursion 1, we're going to talk about it. 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 see how it satisfies two conditions for recursion:

1. For 1 n > 2. To solve for f(n), only f(n-1) and f(n-2) are required. In other words, problems of size n are transformed into smaller problems.

2. For n=0 and n=1, there are simple scenarios: f(0) =0, f(1) =1.

Therefore, we can easily write a recursive program to calculate the n item 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 call to a function, always write the simple situation first to ensure that the call can stop the recursion when it checks the simple situation. Otherwise, your function may never stop the recursive call there.

Determine whether a string is 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 has been helpful to you in programming the C language.


Related articles: