PAT 童年生活二三事 (递推) 详细题解

按理说这就是一道水题, 可我一开始竟然没想出来要用递推, 反而纠结在组合数学和搜索上面了

水题就要多多找找规律, 把前几个答案都列出来, 马上就可以发现就是斐波那契数列了

//童年生活二三事
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 95;

LL n, cont[maxn];
void init()
{
    cont[1] = 1, cont[2] = 2;
    for(int i = 3; i <= maxn; i++)
        cont[i] = cont[i-1]+cont[i-2];
}
int main()
{
    init();
    while(cin >> n){
        cout << cont[n] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/83993145
今日推荐