0
点赞
收藏
分享

微信扫一扫

1-106兔子繁衍问题

code_balance 2022-02-16 阅读 115

我的代码 

 

#include<iostream>

using namespace std;

int birth(int n) {
    if (n == 1)
        return 1;
    int a[3] = {1, 1, 2};
    for (int i = 0, month = 3;; ++month, i = (i + 1) % 3) {
        if (a[(i + 2) % 3] >= n)
            return month;
        a[i] = a[(i + 2) % 3] + a[(i + 1) % 3];     //这个月的兔子=上个月的兔子加上两个月前的兔子
    }


}

int main() {
    int n;
    cin >> n;
    cout << birth(n);
    return 0;
}
举报

相关推荐

0 条评论