面试问题 有50级台阶,你每次可以走一阶或两阶,有多少种走法
程序员文章站
2024-01-13 11:55:40
...
有50级台阶,你每次可以走一阶或两阶,有多少种走法
程序列出所有可能
程序列出所有可能
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class GoStairs { private List<Integer> stepStlye = new ArrayList<Integer>(); public static final int STAIRS_COUNT = 10; public void goStairs(int leftStairsCount) { if (leftStairsCount - 1 >= 0) { stepStlye.add(1); leftStairsCount = leftStairsCount - 1; goStairs(leftStairsCount); leftStairsCount = leftStairsCount + 1; stepStlye.remove(stepStlye.size() - 1); } if (leftStairsCount - 2 >= 0) { stepStlye.add(2); leftStairsCount = leftStairsCount - 2; goStairs(leftStairsCount); leftStairsCount = leftStairsCount + 2; stepStlye.remove(stepStlye.size() - 1); } if (leftStairsCount == 0) { System.out.println(Arrays.toString(stepStlye.toArray(new Integer[0]))); return; } } public static void main(String[] args) { GoStairs ladder = new GoStairs(); ladder.goStairs(STAIRS_COUNT); } }
上一篇: 华为P40系列缔造光学奇迹 再一次树立移动影像新标准
下一篇: 图标,日期组件