GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python_test/test2.py at master · jacquen/python_test Expand file tree/ Copy pathtest2.pyMore file actions 44 lines (31 loc) · 1.14 KB / Copy pathtest2.pyFile metadata and controls44 lines (31 loc) · 1.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 # 练习python的递归函数 # 用递归来求阶乘 def func(num): if num == 1: return 1 return func(num-1)*num # 求100的阶乘 num = 100 n = func(num) print('%d的阶乘是' % (num,), n) # 上面的函数如果n的数字过大的话就会出现栈溢出的问题,这是我们需要通过尾递归来优化 def fact(num, product=1): if num == 1: return product return fact(num-1, num*product) # 虽然做了尾递归优化,但是该语言底层没有弄优化,所以还是求不了1000的阶乘 num = 500 n = fact(num) print('%d的阶乘是' % (num,), n) # 最后的作业,练习汉诺塔:move(n, a, b, c)函数,它接收参数n,表示3个柱子A、B、C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的方法 def move(n, a, b, c): if n == 1: print(a, '-->', c) else: # 借助c柱将a的移动到c move(n-1, a, c, b) # 借助b柱将a的最后一根移动到c move(1, a, b, c) # 最后将b柱的借助a柱移动到c柱 move(n-1, b, a, c) move(3, 'A', 'B', 'C') |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |