7-27 兔子繁衍问题 (15 分)

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_43526304/article/details/85228739

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

输入格式:

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

输出格式:

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

输入样例:

30

输出样例:

9

思路:斐波那契数列 不懂的同学请戳下面链接 https://baijiahao.baidu.com/s?id=1606651492697783298&wfr=spider&for=pc

读懂什么是斐波那契数列 这题也就迎刃而解啦

#include <stdio.h>
int main(){
    int N;
    scanf("%d",&N);
    int month = 1;
    int y = 1;
    int c = 0;
    int n = 1;
    while(n<N){
        int t = c;
        c = c + y;
        y = t;
        n = c + y;
        month ++;
    }
    printf("%d\n",month);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43526304/article/details/85228739
今日推荐