<> 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
Daily Recommendation