/** * 有10阶梯, 每次走1,2 or 3 阶,有多少种方式??? */ public class Test { static final int s = 10; static int len = 0, sum = 0; static int step[] = new int[s]; static void compute(final int stair) { // get result if (stair == 0) { printSum(); sum++; return; } // 步数 1,2 or 3 阶 for (int i = 1; i < 4; i++) { step[len] = i; len++; compute(stair - i); len--; } } static void printSum() { for (int i = 0; i < len; i++) System.out.print(step[i] + " "); System.out.println(); } public static void main(String args[]) { compute(s); System.out.println(sum ); } }