一对兔子,从出生后第3个月起每个月都生一对兔子。小兔子长到第3个月后每个月又生一对兔子。假如兔子都不死,请问第1个月出生的一对兔子,至少需要繁衍到第几个月时兔子总数才可以达到N对?
输入格式:
输入在一行中给出一个不超过10000的正整数N。
输出格式:
在一行中输出兔子总数达到N最少需要的月数。
输入样例:30输出样例:
9
#include <stdio.h>
int total(int month);
int main()
{
int n, month = 1;
scanf("%d", &n);
n *= 2;
while ( total(month) < n ) {
month++;
}
printf("%d\n", month);
return 0;
}
int total(int month)
{
if ( month == 1 || month == 2 ) {
return 2;
}else {
return total(month-1) + total(month-2);
}
}
本博客探讨了一个关于兔子繁殖的数学问题,通过递归函数计算在一定条件下兔子总数达到特定数量所需的最少月数。
2981

被折叠的 条评论
为什么被折叠?



