Rabbit breeding

Rabbit breeding problems

One pair of rabbits from the first 3 months after the birth of one pair of rabbits are born every month. Bunnies grow up to 3 months after the month gave birth to one pair of rabbits. If the rabbit is not dead, I ask the first one was born a rabbit, at least the first few months need to multiply the total number of rabbits before they can reach the N pairs?
Input format:
input line in a given positive integer of not more than 10,000 N.
Output format:
output line reaches the total number of rabbits required minimum number N months.
Sample input:
30
sample output:
9
For this question I started thinking there is no point in the CSDN see most of the bloggers are recursive algorithm for Fibonacci columns, no reading or thinking, until you see this.Here Insert Picture Description

According to this code can be written like this:

#include<stdio.h>
int main()
{
 int month;//未知月份
 int a;//成年兔的对数 
 int b;//兔崽子的对数
 int t;//存储前一个月成年兔子数量 
 int s;//兔子总和
 int n;//兔子数量 
 scanf("%d",&n);
 month=1;//第一个月
 b=1;//兔崽子1对
 a=0;//无成年兔
 t=0;//前一个月成年兔子
 s=1;
 while(s<n){
  month++;//月份递增
  t=a;//上个月成年兔
  a=a+b;//本月成年兔=上月成年兔+上月兔崽子
  b=t;//本月兔崽子=上个月成年兔
  s=a+b;//总和=本月陈年兔+本月兔崽子
}
 printf("%d",month);    
}
Published 17 original articles · won praise 12 · views 261

Guess you like

Origin blog.csdn.net/qq_45894099/article/details/105210325