- 2020-10-22 21:37
*views 2*- algorithm
- Classical algorithm

<> preface

Algorithm is a necessary knowledge for a programmer , from now on , Come and learn the algorithm with me !

<> one , What is recursion

Recursion is the realization of recursion in computer programming , In short, it is a programming skill of program calling itself .

A method in which a procedure or function calls itself directly or indirectly in its definition or description , It usually transforms a large and complex problem into a smaller one similar to the original one , Recursion strategy only needs a few programs to describe the repeated calculation needed in the process of solving problems , It greatly reduces the code of the program

<> two , Use cases

There is a pair of rabbits , From the second day after birth 3 A couple of rabbits are born every month since the first month , The rabbits grow to the third month, and then give birth to a pair of rabbits every month , If the rabbits don't die , What is the total number of rabbits per month ?

First of all, analyze the problem ：

The number of rabbits in the last few months was ：1,1,2,3,5,8,13,21,34,55,89,144, From the data, it can be found that from the third month , The number of rabbits in each month is the sum of the number of rabbits in the first two months

At this time, a recursive condition is satisfied , We can use recursion in the program to achieve such a calculation ：

int GetNub(int n) { int nub; if(n==1||n==2) nub=2; else nub = GetNub(n-1)+

GetNub(n-2); // Invoking itself in programs to simplify problems return nub; }

<> three , How to cultivate recursion thought

Recursion is often accompanied by the development of the timeline , And often before and after a certain connection , If this happens , Consider listing the previous data , And some rules are summarized

Technology

- Python146 blogs
- Java131 blogs
- Vue86 blogs
- Flow Chart79 blogs
- javascript42 blogs
- C++41 blogs
- programing language38 blogs
- MySQL37 blogs
- more...

Daily Recommendation

©2020-2021 ioDraw All rights reserved

2021 Blue Bridge Cup B group C/C++ Personal records How to use entity class to receive get Request parameters for the request filtering algorithm | Arithmetic average filtering method android 10.0 Version integration GMS package cartoon ： What is? JVM Garbage collection in China ? What certificates can big data test ? Recommend a practical Python Data analysis project 2020-1024=? Talking about redis The single point mode of , Master slave mode , Sentry mode and cluster mode USB Drive free NFC Reader writer Android In the system NFC Reading and writing examples