题目链接:Click here
思路:自己写一下就可以看出是个fib数列,递归求解就好。
PS:看动态规划,撇到这个题,想到以前看到了没做出来,所以就试一试,没想到竟然是fib。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
using namespace std;
int fib[46];
int solve(int n)
{
if(n == 1 || n == 2) return 1;
if(fib[n] != -1) return fib[n];
return fib[n] = solve(n-1) + solve(n-2);
}
int main()
{
int n;
memset(fib, -1, sizeof(fib));
fib[1] = fib[2] = 1;
while(scanf("%d", &n) != EOF)
{
if(n == 0)break;
printf("%d\n", solve(n));
}
return 0;
}