Application of recursive thinking

Application of recursive thinking

Introduction to recursive thinking

Recursive thinking is suitable for solving "a problem can be decomposed step by step, and the decomposed sub-problems have the same problem-solving method as the problem", but recursive thinking is not unlimited iteration, but requires us to specify a "base problem" "The basic problem, as its name implies, means "how far do we need to decompose the problem".

Fibonacci sequence

The principle of Fibonacci sequence

 

Code implementation of Fibonacci sequence

#include <iostream>  
using namespace std;  
  
int fibonacci(int n)  
{  
    if (n == 1 || n == 0) // 基问题  
    {  
        return 1;  
    }  
    else if (n >= 2) // 这一系列问题的解决方法  
    {  
        return fibonacci(n - 1) + fibonacci(n - 2);  
    }  
    return 0;  
}  
  
int main()  
{  
    cout << fibonacci(18) << endl;  
}

  

Solve for greatest common divisor

Theoretical Thinking of Solving the Greatest Common Divisor

The toss and turns division method is also called Euclidean algorithm, which is a very old algorithm for solving the greatest common divisor of two numbers. It is based on the principle: two positive integers a and b (a> b), their greatest common divisor gcd is equal to the greatest common divisor between the remainder r and b of dividing a by b. For example, the greatest common divisor 5 of 10 and 25 is equal to the remainder of 25 divided by 10 and the greatest common divisor of 5 and 10; for example, the greatest common divisor of 51 and 21 is equal to the remainder of 51 divided by 21, the greatest common divisor of 9 and 21, The greatest common divisor of 9 and 21 is 3. According to the above principles, the algorithm flow of the tossing and dividing method can be as follows:

Step 1: Calculate the remainder r of a and b.

Step 2: If r is 0, return gcd = b. Otherwise, go to step 3.

Step 3: Use the value of b to update the value of a, use the remainder r to update the value of b, go to step 1.

Code example for solving the greatest common divisor

#include <iostream>  
using namespace std;  
  
int GCD(int m, int n)  
{  
    if (n == 0) // 当被除数为0说明最大公约数为m  
    {  
        return m;  
    }  
    return GCD(n, m%n); // 返回被除数与余数的最大公约数  
}  
  
int main()  
{  
    cout << GCD(19, 3) << endl;  
}  

 

Code analysis

The first part of the code is "determine the base problem for this iteration problem":

  1. if  (n == 0)  //  When the dividend is 0, the greatest common divisor is m  
  2. {  
  3.     return m;  

The second part of the code is "determine the method we want to iterate":

return GCD(n, m%n); // 返回被除数与余数的最大公约数

This code shows: the next step is to solve the greatest common divisor of "dividend n" and "remainder m% n".

Recursive realization of binary search

The principle of dichotomy

https://blog.csdn.net/weixin_45590473/article/details/108683727

Code example of dichotomy using recursion

#include <iostream>  
using namespace std;  
  
bool BinarySearch(int a[], int L, int H, int value)  
{  
    int Middle{ (L + H) / 2 };  
  
    if (H < L) // 基问题  
    {  
        return -1;  
    }  
  
    if (a[Middle] == value) // 基问题  
    {  
        return 1;  
    }  
    else if (a[Middle] > value) // 解决方法  
    {  
        return BinarySearch(a, Middle + 1, H, value);  
    }  
    else if (a[Middle] < value) // 解决方法  
    {  
        return BinarySearch(a, L, Middle - 1, value);  
    }  
}  
  
int main()  
{  
    int a[] = { 1,2,3,4,5 };  
    cout << BinarySearch(a, 0, 4, 2) << endl;  
}  

 

Code analysis

To put it plainly, the basic problem is "the solution of the most basic problem". For the binary tree algorithm, there are two situations: "find the specified element" and "cannot find the specified element". Corresponding to the following code:

    if (H < L) // 基问题  
    {  
        return -1;  
    }  
  
    if (a[Middle] == value) // 基问题  
    {  
        return 1;  
    } 

 

We adjust the search return by comparing the median value in the search range with the target value:

else if (a[Middle] > value) // 解决方法  
{  
    H = Middle;  
    return BinarySearch(a, L, H, value);  
}  
else if (a[Middle] < value) // 解决方法  
{  
    L = Middle;  
    return BinarySearch(a, L, H, value);  
}  

 

 

Guess you like

Origin blog.csdn.net/weixin_45590473/article/details/108715703