/**
* 一个人上楼梯,一次可以迈1步或者2步,问走39层台阶,一共可以有多少种走法?
* 例如: 走3层台阶,那么有1,1,1 1,2 2,1 共3种走法
*/
public class Test39 {
// 1次1步或者2步
public static int[] c = {1, 2};
// 走法数
public int count = 0;
public void dfs(int nowStep, int target) { // nowStep:当前走的数目 target:目标台阶数
if (nowStep >= target) {
if (nowStep == target) {
count++;
}
return;
}
// 回溯
for (int i = 0; i < 2; i++) {
nowStep += c[i];
dfs(nowStep, target);
nowStep -= c[i];
}
}
public static void main(String[] args) {
Test39 t = new Test39();
t.dfs(0, 3); // 3
System.out.println(t.count);
}
}