Collection
zero Useful+1
zero

Recursive

A Method of Describing Complex Problems by Several Repeatable Operations
recursive algorithm It is described by several repeatable operations Complex problems Method. Recursion is a common algorithm in sequence computation. The value of the specified item in the sequence is usually obtained by calculating the previous items.
Chinese name
Recursive
Foreign name
TheRecursive
Definition
A Method of Describing Complex Problems by Several Repeatable Operations
Discipline
mathematics Information Science and System Science Computer Mathematics

Basic Introduction

Announce
edit
Recursion is to calculate each item in the sequence according to certain rules. Usually, the value of the specified item in the sequence is obtained by calculating the previous items. The idea is to transform a complex and huge computing process into multiple iterations of a simple process. The algorithm uses computer Fast and tireless machine features.

recursive algorithm

Announce
edit
[Example 1] Tree planting
Title description:
Arbor Day On that day, five students participated in the tree planting activity, and their trees were different. When asked how many trees the first student planted, he pointed to the second student nearby and said that he had planted two more trees than he did; After questioning the second student, he said that he planted two more trees than the third student In this way, they all said that they planted two more trees than the other student. Finally, when asked about the fifth student, he said he had planted 10 trees. Q: How many trees did the first student plant?
analysis
Let the tree planted by the first student be a1. To seek a1, start with the number of trees planted by the fifth student, a5. According to the rule of "two more trees", calculate in a certain order, that is:
(1) a5=10;
(2) a4=a5+2=12;
(3) a3=a4+2=14;
(4) a2=a3+2=16;
(5) a1=a2+2=18;
Pascal Procedure:
Program Examl; Var i,a:byte; begin a:=10; for i:= 1 to 4 do a:=a+2; writeln('The Num is' ,a); readln; end.
Note: writeln ();
book program The recursive operation of can be represented as follows:
Initial value a=10
a
i
a=10
i=1
a=a+2=12
i=2
a=a+2=14
i=3
a=a+2=16
i=4
a=a+2=18
i=5
Output a value
Python program:
a = 10 for i in range(1, 5): ....a += 2 print(a)
C++Programs:
#include<iostream> using namespace std; int main(){ int stu[10]; for(int i=5; i>=1; i--) stu[i] = stu[i + 1] + 2; cout << stu[1]; return 0; }
[Example 2] Book placement
Title description:
Ten different books are on the shelf. Now rearrange them so that each book is not in the original position. How many pendulum methods are there?
analysis:
When n numbered elements are placed in n numbered positions, the number of methods that the element number and the position number do not correspond to each other is represented by M (n), then M (n-1) means that n-1 numbered elements are placed in n-1 numbered positions, the number of methods that do not correspond to each other, and so on
Step 1: put the nth element in a position, such as position k. There are n-1 methods in total;
The second step is to place the element with the number of k. At this time, there are two cases. 1, place it at position n. Then, for the remaining n-2 elements, there are M (n-2) methods; 2. Don't put it at position n. At this time, there are M (n-1) methods for n-1 elements;
To sum up, we can get:
recursive algorithm Based on the initial (starting point) value, repeat the operation step by step with the same operation rule until the end of the operation. The same method is repeated from the "starting point" until a certain "boundary" is reached. It is like a one-way movement, which can be realized by circulation. The essence of recursion is to deduce (calculate) the results of the first step step by step according to the law.