"Algorithm graphic" chap3 recursive

Cues Cues Notes Notes
  •  Recursion
  • The problem into a recursive baseline conditions and conditions
  • Stack (stack)

One,

  • Cited embodiment: the box, and boxes
  • Solution:
  1. Check the box is everything
  2. If the box is returned to the first step
  3. If the key is, it's done
  • Fake code:
def look_for_key(box):
 for item in box:
  if item.is_a_box():
   look_for_key(item)
  elif item.is_a_key():
   print"found the key"

two,

  • Baseline conditions and recursion conditions: Each has a recursive function
  • Recursive function calls provided that the value of their own
  • Baseline condition is a function no longer call itself

three,

  • Stack data structure is a simple
  • When you call a function in another function, the current function pause and in an unfinished state, the value is still in the memory of all the variables
  • Recursive function call stack:
  •  Fake code:

def fact(x):

 if x==1:

  return 1

 else:

  return x*fact(x-1)

 

Codes

 c++

#include<iostream>
using namespace std;

int recursive(int a)
{
    if(a==1)
        return 1;
    else
        return a*recursive(a-1);
}

int main()
{
    int a=5;
    cout<<recursive(a)<<endl;
    return 0;
}
    

 

Guess you like

Origin www.cnblogs.com/huangyilong/p/11449581.html