Topic - recursion

Recursive topics

The definition : a function calls itself directly or indirectly own

Recursive meet three conditions

  1. Recursive must have a definite termination condition

  2. The size of the data processing function must be decremented

  3. This transformation is solvable

Circulation or Recursion

  • Recursion

    • Readily understood (here is understood in terms of the complex after the knowledge with respect to FIG, trees, etc.)
    • Slow
    • Storage space
  • cycle

    • Difficult to understand
    • Slow
    • Small storage space

Call functions

  • When calling another function during the operation of a function, before running the called function, the system needs three things:

    • Passes all actual parameters, return address and other information to the called function is stored.

    • For the local variables of the called function (including reference lines) allocated storage space.

    • Transfers control to the called function entry.

  • Before being returned from the function call function, the system must accomplish three things:

    • Save the called function returns.
    • Free up storage space occupied by the called function.
    • In accordance with the saved return address of the called function transfers control to the calling function.
  • When there are a plurality of function calls to each other, according to "return to the first call" principle, and the transmission of information between the control transfer functions have to rely on "stack" is achieved, i.e. the system will be required to run the entire program schedule data in space a stack, whenever a function is called, it is assigned a storage area in the top of the stack, perform push operation, whenever a function exits, it releases its storage area, do pop operations, functions currently running forever in the stack position.

  • A function and function calls A B A function calls a function in the computer appears to be no different, but with our daily way of thinking to understand more bizarre it!

  1. 1 + 2 + 3 + ... + n, and
int sum(int n)
{
   if(n==1)
   	return 1;
   else
  		return n+sum(n-1);
}
  1. Factorial
int jc(int n)
{
    if(n==1)
        return 1;
    else
        return n*jc(n-1);
}


  1. Fibonacci 11235813 ....
int fs(int day)
{
    if((day==1)||(day==2))
        return 1;
    else
        return fs(n-1)+fs(n-2);
}
  1. Tower of Hanoi

Algorithm Pseudo
If only one plate, directly from the A -> C
if n disks, n> 1, the n-1 first tray moves from A to B via C
and then moved to the n-th tray C
then n- a plate moves from B to A by C

//2020.4.7//0:20

#include<iostream>
using namespace std;

void hanoi(int, char, char, char);

int main()
{	
   //盘子个数以及编号(第一个就是1,最后一个就是n)
   int n;
   cout << "请输入盘子个数" << endl;
   cin >> n;

   //柱子编号(其实没有写的必要)
   char ch1 = 'A';
   char ch2 = 'B';
   char ch3 = 'C';
   
   hanoi(n, 'A', 'B', 'C');  //将n个盘子从A柱子借助中间柱子B转移到C
}

void hanoi(int n, char from, char depend, char to)//记住位置 第一个位置是初始塔,第二个是中转塔,第三个是目的塔
{
   if (n == 1)
   	cout << "编号为" << n << "的盘子从" << from << "————>" << to << endl;//一个就直接移好了
   else
   {
   	//把最下面第n个上面n-1个盘子全部从A借助C移动到B
   	hanoi(n - 1, from, to, depend);
   	//再把第n个盘子从A移至C
   	cout << "编号为" << n << "的盘子从" << from << "————>" << to << endl;
   	//再把n-1个盘子全部从B借助A移动至C
   	hanoi(n - 1, depend, from, to);
   }
}

  

Guess you like

Origin www.cnblogs.com/yuuuuu422/p/12650845.html