
# 第一种方法:
rb1 = 1
rb2 = 1
month = int(input("请输入月份:"))
if month == 1 or month == 2:
print(f'{month}月的兔子有1对')
else:
rb = 0
for i in range(month-2):
# 下个月的兔子数rb = 上上个月的兔子数rb1 + 上个月的兔子数rb2
rb = rb1 + rb2
rb1 = rb2
rb2 = rb
print(f'{month}月的兔子有{rb}对')
# 第二种方法:(递归)列表
num_list = [1,1]
month = int(input("请输入月份:"))
if month == 1 or month ==2:
print(f'{month}月的兔子有1对')
else:
for i in range(month-2):
# 如何在列表中添加元素用:列表名.append()
# [-1]代表列表中的最后一个元素,[-2]是列表中倒数第二个元素依此类推
num_list.append(num_list[-1] + num_list[-2])
print(f'{month}月的兔子有{num_list[-1]}对')
#第三种方法:(定义方法)
def rb_count(month):
if month == 1 or month == 2:
return 1
else:
return rb_count(month-1)+rb_count(month-2)
month =int(input('请输入月份:'))
print(f'{month}月的兔子有{rb_count(month)}对')