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

# 计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出:

# fact(n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! x n = fact(n-1) x n

# 使用递归函数的优点是逻辑简单清晰,缺点是过深的调用会导致栈溢出。

def fact(n):
if n==1:
return 1
return n * fact(n-1)
print('fact(1) =', fact(1))
print('fact(5) =', fact(5))
print('fact(10) =', fact(10)) # 利用递归函数移动汉汉诺塔:
def move (n, a, b, c):
if n == 1:
print('move', a, '-->', c) else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')

廖雪峰网站:学习python函数—递归函数(四)的更多相关文章

  1. python初步学习-python函数(一)

    python 函数 函数是组织好的,可重复使用的,用来实现单一或者相关联功能的代码段. 函数能提高应用的模块性和代码的重复利用率. 函数定义 python中函数定义有一些简单的规则: 函数代码块以de ...

  2. 廖雪峰网站:学习python函数—调用函数(一)

    # 调用函数 # 可以直接从Python的官方网站查看文档: # http://docs.python.org/3/library/functions.html#abs n = abs(100) # ...

  3. 廖雪峰网站:学习python函数—函数参数(三)

    1.*args # 位置参数,计算x2的函数 def power(x): return x * x p = power(5) print(p) # 把power(x)修改为power(x, n),用来 ...

  4. 廖雪峰网站:学习python函数—定义函数(二)

    def my_abs(x): if x >= 0: return x else: return -x print(my_abs(-99)) # 空函数 def nop(): pass # 参数检 ...

  5. 廖雪峰网站:学习python基础知识—循环(四)

    一.循环 1.for names = ['Michal', 'Bob', 'tracy'] for name in names: print(name) sum = 0 for x in [1, 2, ...

  6. 廖雪峰网站:学习python基础知识(一)

    1. python能做什么? 可以做日常任务,比如自动备份你的MP3:可以做网站,很多著名的网站包括YouTube就是Python写的:可以做网络游戏的后台,很多在线游戏的后台都是Python开发的. ...

  7. 廖雪峰网站:学习python基础知识—list和tuple(二)

    1.list """ Python内置的一种数据类型是列表:list. list是一种有序的集合,可以随时添加和删除其中的元素. """ c ...

  8. 廖雪峰网站:学习python基础知识—判断(三)

    一.判断 1.条件判断 age = 18 if age >= 18: print('your are is', age) print('adult') age = 3 if age >= ...

  9. 廖雪峰 JavaScript 学习笔记(函数)

    JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述abs()函 ...

随机推荐

  1. javbus爬虫-老司机你值得拥有

    # 起因 有个朋友叫我帮忙写个爬虫,爬取javbus5上面所有的详情页链接,也就是所有的https://www.javbus5.com/SRS-055这种链接, 我一看,嘿呀,这是司机的活儿啊,我绝对 ...

  2. C语言 字符串大小写转换 自定义函数

    #include <stdio.h>#include <stdlib.h>#include <string.h> char * strtolower(char * ...

  3. 常用linux,DOS命令——持续更新

    cd 文件夹名 进入某个文件夹 cd ../ 退出该级目录进入上一级 cd ../../ 退出该级目录进入上上级 cd ../../demo 退出该级目录进入上上级的目录 d: 回车 进入d盘 ls ...

  4. Footnotes for tables in latex - 为latex的table加上footnotes

    参考: Footnotes for tables in LaTeX Footnote in tabular environment Footnotes for tables in latex - 为l ...

  5. 20165306学习基础和C语言基础调查

    20165306学习基础和C语言基础调查 技能学习心得 我认为兴趣.责任感.毅力对技能的获得非常重要. 因为我从小五音不全.肢体不协调,所以看春晚等节目的时候会把更多的关注点放在主持人身上.小时候觉得 ...

  6. python学习 day016打卡 面向对象--成员

    本节主要内容: 1.类的成员 2.类的成员-变量 3.类的成员-方法 4.类的成员-属性 5.私有 一.类的成员: 能在类中写的内容就是类的成员. class 类名: #方法 def __init__ ...

  7. mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...

  8. PL/SQL Developer过期解决方法

    参考资料: plsql过期解决方法 plsql永久注册码适用个版本 方法一: 1.首先,登陆PL/SQL Developer,PL/SQL Developer要到期了 2.输入指令“regedit”打 ...

  9. idea使用教程(1)

    引言:本教程主要讲解一下常用的配置安装方法,不包含软件安装,按照以下教程配置后,可以直接用于生产环境. 参考网址:参考了尚硅谷关于idea的使用教学视屏 idea注册码地址:http://idea.l ...

  10. leecode第四十三题(字符串相乘)

    class Solution { public: string multiply(string num1, string num2) { ";//特殊情况 ] == ] == ') retu ...