统计每个月兔子的总数(HJ37)

一:解题思路

这道题目的本质就是求斐波那契数列的第n项。

二:完整代码示例 (C++版和Java版)

C++代码:

#include <iostream>

using namespace std;

int main()
{
    int months = 0;

    while (cin >> months)
    {
        int f1 = 1;
        int f2 = 1;
        for (int i = 2; i < months; i++)
        {
            int f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }

        cout << f2 << endl;
    }

    return 0;
}

 

posted @ 2020-08-05 17:55  repinkply  阅读(511)  评论(0)    收藏  举报