definition (Personal understanding)
1. Call yourself smaller than yourself on a scale.
2. There is an end condition.
3. Refinement of the problem.
PS: You can feel the recursion through this effect.
Drost Effect:
*************************************************************************************************************** *********************************
Recursive conditionsOf course not all problems can be achieved with recursion. To be able to implement recursion, the following conditions need to be met:
1. There is a recursive formula. The problem can be broken down into a small problem that is similar to itself. 2. There is a definite border. Can finally be decomposed into a problem with definite solution.
Auxiliary Analysis diagram:
Personally, this figure should be able to reflect the recursive call more clearly. We can think of recursive invocation as the code that adds the F () function to the original F () function, except that the parameters are different from the original. Only when the boundary is reached will the function be returned one layer at a time. That is, when a problem is completed and returned, the outermost function is actually completed and returned.
Don't quite understand. It doesn't matter, let's understand recursion ~ ********************************************************************************************** from the example **************************************************
Classic Example (personal analysis)The typesetting from easy to difficult, please viewers according to their own needs to flip through.
factorial
Problem Description: Please enter an n value, output the value of n!.
Code
Operation Result:
Variable 1: Complete Fibonacci sequence 2: Complete Ackerman function (answer at last ~)
Do you think recursion is simple. Let's go on.
*************************************************************************************************************** *
Hanoi Tower Problem
Problem Description: Suppose there are 3 towers named X, Y, and Z, and the size of n is different on Tower X, which is numbered from small to large, ..., n (as shown in the figure). It is now required to move n discs on the x axis to tower Z and still stack in the same order, the following rules must be followed when the disc moves: (1) Only one disc can be moved at a time, and (2) the disc may be inserted on any of the towers in X, Y and Z, and (3) a larger disc is not pressed on the smaller disc at any time. How to move, please. How many times you want to move.
Code
Operation Result:
If you feel a bit dizzy, you can also: 1. Take the first explanation to see, that is, the original function in the beginning of the code added. 2. Bring in the actual data and run it through the program. 3. Take a look at the following motion diagram ~
Hanoi Stack Method Solution: http://www.cricode.com/304.html
*************************************************************************************************************** *
Eight Queen's question
Problem Description: The classic eight Queen question, which is to put 8 queens on a 8*8 board, so that the 8 Queens cannot attack each other (any 2 queens cannot be on the same row, in the same column or on the diagonal), outputting all possible placement conditions.
Code
#include <iostream>
using namespace std;
int c[20],n=8,cnt=0;
void print () {//Print for
(int. i=0;i<n;++i) {for
(int j=0;j<n;++j) {
if (J==c[i]) cout<< "1";
else cout<< "0";
}
cout<<endl;
}
cout<<endl;
}
void search (int r) {
if (r==n) {//The position must be fixed when the last queen has been discharged. "Condition 2"
print ();
++cnt;
return;
}
for (int i=0;i<n;++i) {//r denotes the first few lines
of c[r]=i;//c[r]=i, which represents the first r row of column I,
int ok=1;
for (int j=0;j<r;++j)
if (c[r]==c[j]| | r-j==c[r]-c[j]| | R-j==c[j]-c[r]) {//Determine if the same column, whether on the main diagonal or diagonal "condition 1"
ok=0;
break;
}
if (OK) search (r+1); The r+1 must not be in the same row, so it is not necessary to determine if it is in a row
}
int main () {
search (0);
cout<<cnt<<endl;
return 0;
}
Running Results Section screenshot:
****************************************************************************************************
Answer: Variable 1: Use formula F[n]=f[n-1]+f[n-2], recursive calculation, recursive end condition is f[1]=1,f[2]=1.
Variable 2: Use formula Ack (m,n) =ack (M-1,ack (m,n-1)), recursive calculation recursively, recursive end condition n=0, ACK (m,n) =ack (m-1,1), m=0 ack (m,n) =n+1.