一對兔子,從出生后第3個月起每個月都生一對兔子。小兔子長到第3個月后每個月又生一對兔子。假如兔子都不死,請問第1個月出生的一對兔子,至少需要繁衍到第幾個月時兔子總數才可以達到N對?
輸入格式:
輸入在一行中給出一個不超過10000的正整數N。
輸出格式:
在一行中輸出兔子總數達到N最少需要的月數。
————————————————
/* 列表說明關系
month | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
number | 1 | 1 | 2 | 3 | 5 | 8 | 13 |
規律:第n個月的兔子數是n-2月+n-1月的兔子的和
*/
#include <stdio.h>
int main (){
int N,month=1,i,number=1;
scanf("%d",&N);
int count [2];
while (number < N) {
i = month%2;
if (month <= 2) { // 前兩位為1
count [i]=number;
continue; // 也可在下面使用else語句,月份大於二時。
}
number = count[0] + count[1];
count[0] = count [1];
count[1] = number;
month ++;
}
printf("%d\n",month);
return 0;
}