程序如下:

def rabbit_count(n):
    if n == 1 or n == 2:  # 第一个月和第二个月只有一对兔子
        return 1
    else:
        return rabbit_count(n-1) + rabbit_count(n-2)  # 第n个月的兔子数等于前两个月的兔子数之和

month = int(input("请输入月份:"))
total_rabbit = 0

for i in range(1, month+1):
    total_rabbit += rabbit_count(i)

print("第{}个月时兔子的总数为:{}".format(month, total_rabbit))

运行程序后,输入月份,即可得到该月份时兔子的总数。

古典问题:兔子繁殖问题 - Python代码实现

原文地址: https://www.cveoy.top/t/topic/pZ5L 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录