題目鏈接:點擊打開鏈接
解題思路:
上來先把n 分別爲1、2、3、4的情況大致列了一下,發現n == 1時結果爲2,n== 2時結果爲2,n == 3時結果爲4,n== 4時結果爲6.
於是大膽的猜想ans[i] = ans[i - 1] + ans[i - 2],但是WA在#12。把預處理表打出來看了看······當n達到45時,結果都溢出了,並且此題long long存不下,果斷開unsigned long long。
完整代碼:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
int n;
const int maxn = 55;
unsigned long long ans[maxn];
int main()
{
#ifdef DoubleQ
freopen("in.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(0);
ans[1] = ans[2] = 2;
for(int i = 3; i < maxn ; i ++)
ans[i] = ans[i - 1] + ans[i - 2];
while(cin >> n)
{
cout << ans[n] << endl;
}
}
更多精彩請訪問:點擊打開鏈接