This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#include <bits/stdc++.h>
using namespace std;
int n;
int sum;
/**
* @param level 现在站在第几阶台阶上
* N阶楼梯上楼问题:一次可以走两阶或一阶,请把所有行走方式打印出来。
* 测试数据: 5 输出结果 一共有8种走法
* 测试数据: 15 输出结果 一共有987种走法
* 方案 :递归
*/
void dfs(int level) {
//如果可以走到第n个台阶,表示可以成功登顶,算是一个解决方案
if (level == n) sum++;
//2种分枝
for (int i = 1; i <= 2; i++)
if (level + i <= n) dfs(level + i);
}
int main() {
cin >> n;
dfs(0);
printf("一共 %d 种方法。\n", sum);
return 0;