函数递归(斐波那契、兔子繁殖、回文判断)

函数递归

什么是递归?

递归就是递归式的函数

从前有座山,山里有座庙,庙里有个老和尚讲故事,讲的什么故事呢?
从前有座山,山里有座庙,庙里有个老和尚讲故事,讲的什么故事呢?…

递归简单理解就是自己去引用自己!
递归式函数,在函数中自己调用自己!

无穷递归,如果这个函数被调用,程序的内存会溢出,效果类似于死循环
def fn():
fn()
fn()


递归是解决问题的一种方式,它和循环很像
它的整体思想是,将一个大问题分解为一个个的小问题,直到问题无法分解时,再去解决问题

递归式函数的两个要件
1.基线条件

  • 问题可以被分解为的最小问题,当满足基线条件时,递归就不在执行了

2.递归条件

  • 将问题继续分解的条件

递归和循环类似,基本是可以互相代替的

  • 循环编写起来比较容易,阅读起来稍难
  • 递归编写起来难,但是方便阅读

1、斐波那契

问题:求第n个斐波那契数列的值(n>2)

斐波那契额数列(Fibonacci sequence):0、1、1、2、3、5、8、13、21、34;

分析:从F(2)开始,结果为前两个数之和;即F(0)=0;F(1)=1;F(2) = F(0) + F(1) ;F(3)=F(1)+F(2)

def Fibonacci(n):
	if n == 0:
		return 0
	elif n == 1:
		return 1
	else:
		return Fibonacci(n-1) + Fibonacci(n-2)


while True:
	num = int(input("please input a number:"))
	if num < 0:
		print("Error!!!")
		break
	else:
		print(Fibonacci(num))

执行结果:
D:\网络安全\Python\py_code>python Class7斐波那契额数列.py
please input a number:0
0
please input a number:1
1
please input a number:2
1
please input a number:3
2
please input a number:4
3
please input a number:5
5
please input a number:6
8
please input a number:7
13
please input a number:-1
Error!!!

D:\网络安全\Python\py_code>

2、兔子繁殖

问题:有一对刚出生的兔子,经过四个月成长为一对成熟的兔子,成熟后,每月生出一对兔子,若不考虑兔子死亡,第n月,共有多少对兔子?

分析:四个月产生出一对新的兔子,数据如下

月数  兔子对数
1		1
2		1
3		1
4		1
5		2  第一个月的兔子经4个月成熟,生出一对兔子
6		3  每月生出一堆兔子
7		4
8		5
9		7  第5个月出生的新兔子经4个月成熟,生出新兔子
10		10  
...以此类推

可以明显的得出数据的规律,即第n个月的兔子数量为,前一个月的数量+前四个月的数量

伪代码:

当n小于等于4时 数量为1

当n大于4时 数量为F(n-1)+F(n-4)

def rab(n):
	if 1 <= n <= 4:
		return 1
	else:
		return rab(n-1) + rab(n-4)


while True:
	month = int(input("please input the month:"))
	if month <= 0:
		print("Error!!!")
		break
	else:
		print(rab(month))

执行结果:
D:\网络安全\Python\py_code>python Class7兔子繁殖.py
please input the month:1
1
please input the month:2
1
please input the month:3
1
please input the month:4
1
please input the month:5
2
please input the month:6
3
please input the month:7
4
please input the month:8
5
please input the month:9
7
please input the month:10
10
please input the month:11
14
please input the month:12
19
please input the month:0
Error!!!

3、回文判断

说明:创建一个函数,用来检查一个任意的字符串是否是回文字符串,如果是返回True,否则返回False

回文字符串:字符串从前往后念和从后往前念是一样的。类似与如下这种

abcba
abcdefgfedcba

分析:

1、先检查第一个字符和最后一个字符是否一致,如果不一致则不是回文字符串

2、 如果一致,则看剩余的部分是否是回文字符串

检查 abcdefgfedcba 是不是回文
检查 bcdefgfedcb 是不是回文
检查 cdefgfedc 是不是回文
检查 defgfed 是不是回文
检查 efgfe 是不是回文
检查 fgf 是不是回文
检查 g 是不是回文

def hui_wen(s):
    '''
        该函数用来检查指定的字符串是否回文字符串,如果是返回True,否则返回False

        参数:
            s:就是要检查的字符串
    '''
    # 基线条件
    if len(s) < 2 :
        # 字符串的长度小于2,则字符串一定是回文
        return True
    elif s[0] != s[-1]:
        # 第一个字符和最后一个字符不相等,不是回文字符串
        return False    
    # 递归条件    
    #将字符串切片,从1开始切,那么去除掉了索引为0的元素;切到-1,即索引最后的元素,那么就不包含最后一个元素,这样一来,就切除了字符串的第一个和最后一个元素
    return hui_wen(s[1:-1])

#调用函数
print(hui_wen('abcdcba'))

执行结果:
True

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值