标签:int pre def ret else NPU style == 小兔子
迭代实现
def fab(n): n1 = 1 n2 = 1 n3 = 1 if n < 1: print(‘输入有误!‘) return -1 while (n - 2) > 0: n3 = n2 + n1 n1 = n2 n2 = n3 n -= 1 return n3 daylop = int(input(‘请输入月数:‘)) result = fab(daylop) if result != -1: print(‘总共有%d对小兔子诞生!‘ % result)
递归
def fab(n): if n < 1: print(‘输入有误!‘) return -1 if n == 1 or n ==2: return 1 else: return fab(n-1) + fab(n-2) dalop = int(input(‘请输入月数:‘)) result = fab(dalop) if result !=-1: print(‘总共或有%d对兔子‘% result)
标签:int pre def ret else NPU style == 小兔子
原文地址:https://www.cnblogs.com/Essaycode/p/10054290.html