求站外题解法
  • 板块灌水区
  • 楼主xht_37
  • 当前回复10
  • 已保存回复10
  • 发布时间2024/4/4 07:18
  • 上次更新2024/4/4 11:27:18
查看原帖
求站外题解法
1085460
xht_37楼主2024/4/4 07:18

楼梯有 nn 阶台阶,上楼时可以一步上 11 阶,也可以一步上 22 阶,也可以一步上 33 阶,编程计算共有多少种不同的走法。(71 > n > 0,输入的每一行包括一组测试数据,即为台阶数n。 最后一行为0,表示测试结束。)

我的暴力代码:

#include <iostream>
#include <cmath>
int dfs(int x, int n) {
	if (x == n) return 1;
	if (x > n) return 0;
	int step1 = dfs(x + 1, n);
	int step2 = dfs(x + 2, n);
	int step3 = dfs(x + 3, n);
	return step1 + step2 + step3;
}
int main() {
	int n;
	std::cin >> n;
	while (n != 0) {
		std::cout << dfs(0, n) << std::endl;
		std::cin >> n;
	}
	return 0;
}
2024/4/4 07:18
加载中...