递归:程序调用自身

形式:在函数定义有直接或间接调用自身

阶乘:N!=123...N

def p(n):
x = 1
i = 1
while i <= n:
x = x * i
i = i + 1
return x

n!=(n-1)! * n

...

兔子数列

斐波那契数列



def fib(n):
if n == 1 or n == 2:
return 1
else :
return fib(n - 1) + fib(n - 2) print fib(6)

汉诺塔

count = 0#步骤数    x**n - 1
def hanoi(n,A,B,C):
global count
if n == 1:
print 'Move',n,'from',A,'to',C
count += 1
else:
hanoi(n - 1,A,C,B)
print 'Move',n,'from',A,'to',C
count += 1
hanoi(n - 1,B,A,C) hanoi(5,'Left','Mid','Right')
print count

随机停车

#随机停车
import random def parking(low,high):
if high - low < 1:
return 0
else :
x = random.uniform(low,high - 1)
return parking(low,x) + 1 + parking(x + 1,high) s = 0
for i in range(1000):
s += parking(0,5) print s / 10000.

Renyi停车常数

递归的时间开销

递归的优劣分析

优势strength

它能使一个蕴含递归关系且结构复杂的程序简洁精炼,增加可读性
特别是在难于找到从边界到解的全过程的情况下,如果把问题推进一步,其结构仍维持原问题的关系

劣势weakness

嵌套层次深,函数调用开销大
重复计算

note 7 递归函数的更多相关文章

  1. Python学习札记(十四) Function4 递归函数 & Hanoi Tower

    reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...

  2. 三星Note 7停产,原来是吃了流程的亏

    三星Note 7发售两个月即成为全球噩梦,从首炸到传言停产仅仅47天.所谓"屋漏偏逢连天雨",相比华为.小米等品牌对其全球市场的挤压.侵蚀,Galaxy Note 7爆炸事件这场连 ...

  3. 《Note --- Unreal --- MemPro (CONTINUE... ...)》

    Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...

  4. 《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》

    ---------------------------------------------------------------------------------------------------- ...

  5. javascript中的递归函数

    正常的递归函数如下: function factorial(num){ ){ ; }else{ ); } } 这个函数表面看起来还ok,但如果我们执行下面代码就会出错. var jenny = fac ...

  6. [LeetCode] Ransom Note 赎金条

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  7. Beginning Scala study note(9) Scala and Java Interoperability

    1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...

  8. Beginning Scala study note(8) Scala Type System

    1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...

  9. Beginning Scala study note(7) Trait

    A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...

随机推荐

  1. python爬虫初级--获取指定页面上的菜单名称以及链接,然后导出

    ''' Created on 2017年4月5日 @author: Admin ''' import requests from bs4 import BeautifulSoup as bsp # 网 ...

  2. Java中8种常见的排序方法

    排序方法的演示1)插入排序(直接插入排序.希尔排序)2)交换排序(冒泡排序.快速排序)3)选择排序(直接选择排序.堆排序)4)归并排序5)分配排序(基数排序)所需辅助空间最多:归并排序所需辅助空间最少 ...

  3. Linux 驱动——LED(驱动分离分层)

    led_dev.c文件: #include <linux/module.h>#include <linux/version.h> #include <linux/init ...

  4. Findout之为什么公司内部不能使用SSH协议连接外网服务器

    今天在公司学习Linux的过程中,想试着像在Windows中操作Github一样对代码进行克隆,只不过是使用命令行的方式.根据一篇博文(Linux下初次使用Github配置)进行了配置,当我进行到第二 ...

  5. header头 下载文件 参数详解

    header( header( header( header( header( header( header( header( header( //2 浏览器不会响应缓存 //1 Public指示响应 ...

  6. 非对称加密, 助记词, PIN, WIF

    一钱包 1.1非对称加密, 助记词, PIN, WIF, 地址 1.1.1 非对称加密算法 非对称加密算法, 加密与解密使用不同的KEY, 我们分别称为私钥与公钥,其中可以通过私钥生成公钥 在比特币中 ...

  7. C语言权威指南和书单 - 中等级别

    注:点击标题免费下载电子书 1. Object-oriented Programming with ANSI-C 2. C Interfaces and Implementations 3. 21st ...

  8. win10磁盘/cpu占用100%问题

    https://www.zhihu.com/question/27664545 https://www.zhihu.com/question/27664545 微软自己的解决办法 https://su ...

  9. django_视图层_编写url

    URL的配置 django中,url也称urlconf,默认的django项目设定两个url地址,分别是admin站点管理和首页地址.from diango.urls import path,incl ...

  10. Linux文件编辑时光标操作

    一.移动光标类命令 h :光标左移一个字符 l :光标右移一个字符 space:光标右移一个字符 Backspace:光标左移一个字符 k或Ctrl+p:光标上移一行 j或Ctrl+n :光标下移一行 ...