面试问题 有50级台阶,你每次可以走一阶或两阶,有多少种走法

有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);
	}
}

猜你喜欢

转载自solong1980.iteye.com/blog/2391210