python 之 递归
注:
参考:https://www.runoob.com/python3/python3-examples.html
参考:《算法图解》
环境: Visual Code Python2.7
阶乘
# -*- coding:UTF-8 -*- #!/usr/bin/env python import sys import math ‘‘‘ 设置递归深度,否则在递归阶乘1000时,会报错: python maximum recursion depth exceeded(超过python递归深度) ‘‘‘ sys.setrecursionlimit(2000) # 递归阶乘 def Recursion_factorial(num): if num > 1: return num * Recursion_factorial(num - 1) return 1 # python数学库 def Math_factorial(num): return math.factorial(num) if __name__ == ‘__main__‘: selectIndex = input(u‘请选择计算的方式(1-递归 2-数学库):‘.encode(‘gbk‘)) num = input(u‘请输入数字:‘.encode(‘gbk‘)) if selectIndex == 1: result = Recursion_factorial(num) print(result) elif selectIndex == 2: result = Math_factorial(num) print(result)
相关推荐
steeven 2020-11-10
Tips 2020-10-14
nongfusanquan0 2020-08-18
yedaoxiaodi 2020-07-26
清溪算法君老号 2020-06-27
pengkingli 2020-06-25
yishujixiaoxiao 2020-06-25
清溪算法 2020-06-21
RememberMePlease 2020-06-17
nurvnurv 2020-06-05
SystemArchitect 2020-06-02
码墨 2020-05-29
清溪算法 2020-05-27
choupiaoyi 2020-05-27
清溪算法 2020-05-25
bluewelkin 2020-05-19
dbhllnr 2020-05-15
steeven 2020-05-09
baike 2020-05-09