Hi everyone, hello! I am a cheese who loves to touch fish ❤
Here comes the whole small case~
It's about raising rabbits,
Let's analyze from ordinary logical thinking
Then turn into programming thinking~
Topic : There is a pair of rabbits,
a pair of rabbits will be born every month from the third month after birth, and a pair
of rabbits will be born every month after the baby rabbit grows to the third month.
If the rabbits are not dead, ask each month What is the total number of rabbits in ?
analyze:
The 0th month is born,
the January rabbit is full moon,
the February rabbit is 2 months old, the March
rabbit is 3 months old, and
the adult rabbit is 3 months old.
The month and number of rabbits that can be pushed forward a few months:
month = int(input('繁殖几个月?: '))
month_0 = 1
month_1 = 0
month_2 = 0
month_elder = 0
for i in range(month):
month_0, month_1, month_2, month_elder = month_elder + month_2, month_0, month_1, month_elder + month_2
print('第%d个月共' % (i + 1), month_0 + month_1 + month_2 + month_elder, '对兔子')
print('其中0月兔:', month_0)
print('其中1月兔:', month_1)
print('其中2月兔:', month_2)
print('其中成年兔:', month_elder)
I don’t know if you see this,
do you have a little grasp of the syntax of python?
繁殖几个月?: 6
第1个月共 1 对兔子
其中0月兔: 0
其中1月兔: 1
其中2月兔: 0
其中成年兔: 0
第2个月共 1 对兔子
其中0月兔: 0
其中1月兔: 0
其中2月兔: 1
其中成年兔: 0
第3个月共 2 对兔子
其中0月兔: 1
其中1月兔: 0
其中2月兔: 0
其中成年兔: 1
第4个月共 3 对兔子
其中0月兔: 1
其中1月兔: 1
其中2月兔: 0
其中成年兔: 1
第5个月共 4 对兔子
其中0月兔: 1
其中1月兔: 1
其中2月兔: 1
其中成年兔: 1
第6个月共 6 对兔子
其中0月兔: 2
其中1月兔: 1
其中2月兔: 1
其中成年兔: 2