zoukankan      html  css  js  c++  java
  • 递归

    定义:在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。


    递归特性:
      1,必须有明确的结束条件
      2,每进入一层,都应该比上一层的递归次数少
      3,把握递归次数,每进入一层,栈帧就会增加一层,每退出一层,栈帧就会减少一层
        由于栈不是无限大的,防止栈溢出
    递归求阶层:

     def cal(n):

        if n== 1:
    return 1
    return n*cal(n-1)


    print(cal(5))


    递归练习:
    二分查找
    data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35]

    def k(dataset,n):
    print(dataset)

    if len(dataset) > 1:
    mid = int(len(dataset) / 2)

    if dataset[mid] == n:
    print('this is the answer')

    elif dataset[mid] > n:
    print('this one is bigger than n')
    return k(dataset[:mid],n)

    else:
    print('this one is smaller than n')
    return k(dataset[mid+1:],n)

    else:
    if dataset[0] == n:
    print('this is the answer')

    else:
    print('n is not in the dataset')
    k(data,5)
  • 相关阅读:
    REP开发技巧
    css grid栅格布局
    flex学习, 尝试布局一个计算器
    sublime text html插件emmet
    flex布局
    SQL Server为字段添加默认值
    windows和linux文件输
    python eric6 IDE
    git撤销修改
    pyQt5
  • 原文地址:https://www.cnblogs.com/christmassa/p/9037491.html
Copyright © 2011-2022 走看看