统计每个月兔子的总数(C++牛客网)

解题思路:

(1)菲波那切数列

#include<iostream>
using namespace std;
int helper(int n) {
    if(n<=2) return 1;
    int a=1,b=1,c;
    int i=3;
    while(i<=n) {
        c=a+b;
        a=b;
        b=c;
        i++;
    }
    return c;
}
int main() {
    int n;
    while(cin>>n) {
        cout<<helper(n)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114962402