Python编程练习010:兔子生兔子
2020-05-02 本文已影响0人
Yang_6234
![](https://img.haomeiwen.com/i19799993/93de894771575b9c.jpg)
题目
古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
兔子的规律为数列1,1,2,3,5,8,13,21....
实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
f1 = 1
f2 = 1
for i in range(1,22):
print '%12ld %12ld' % (f1,f2),
if (i % 3) == 0:
print ''
f1 = f1 + f2
f2 = f1 + f2
以上实例输出结果为:
1 1 2 3 5 8
13 21 34 55 89 144
233 377 610 987 1597 2584
4181 6765 10946 17711 28657 46368
75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352
24157817 39088169 63245986 102334155 165580141 267914296
方法二:
month=int(input('繁殖几个月?: '))
month_1=1
month_2=0
month_3=0
month_elder=0
for i in range(month):
month_1,month_2,month_3,month_elder=month_elder+month_3,month_1,month_2,month_elder+month_3
print('第%d个月共'%(i+1),month_1+month_2+month_3+month_elder,'对兔子')
print('其中1月兔:',month_1)
print('其中2月兔:',month_2)
print('其中3月兔:',month_3)
print('其中成年兔:',month_elder)