您的位置:首页 > 其它

循环-05. 兔子繁衍问题(15)

2015-06-09 23:38 309 查看
一对兔子,从出生后第3个月起每个月都生一对兔子。小兔子长到第3个月后每个月又生一对兔子。假如兔子都不死,请问第1个月出生的一对兔子,至少需要繁衍到第几个月时兔子总数才可以达到N对?

输入格式:

输入在一行中给出一个不超过10000的正整数N。

输出格式:

在一行中输出兔子总数达到N最少需要的月数。

输入样例:

30

输出样例:

9

#include <stdio.h>
int fibonacci(int number)
{
int f1 = 1, f2 = 1, f3 = 1;
if(number <2) {
f3 = 1;
} else {
while(number-2>0) {
f3 = f2 + f1;
f1 = f2;
f2 = f3;
number --;
}

}
return f3;
}
int main(int argc, char const *argv[])
{
int cnt = 1, n;
int i;
scanf("%d", &n);
for(i = 1; fibonacci(i) < n; i ++) {
cnt ++;
}
printf("%d\n", cnt);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: