【问题描述】
小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前 的台阶数,恰好是39级!
站在台阶前,他突然又想着一个问题:
如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?
请你利用计算机的优势,帮助小明寻找答案。
【答案提交】
要求提交的是一个整数。
注意:不要提交解答过程,或其它的辅助说明文字。
题解:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int ans;
// total表示当前走过的台阶数量,step表示当前的步数
void dfs(int total, int step)
{
// 超出边界
if(total > 39) return;
if(total == 39)
{
// 步数为偶数,计数器加一
if(step % 2 == 0) ans ++;
return ;
}
// 每次迈步都有两种选择
dfs(total + 1, step + 1);
dfs(total + 2, step + 1);
}
int main()
{
dfs(0, 0);
cout << ans << endl;
return 0;
}
答案:51167078