Java第39级台阶-记忆搜索
小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,
恰好是39级!
站在台阶前,他突然又想着一个问题:
如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,
也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?
请你利用计算机的优势,帮助小明寻找答案。
static int arr[][] = new int[40][2];
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis(); // 获取开始时间
System.out.println(fun(39, 0));
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
}
public static int fun(int n, int tep) {
if (n < 0)
return 0;
if (arr[n][tep] != 0)
return arr[n][tep];
if (n == 0 && tep == 0) {
arr[n][tep] = 1;
return 1;
}
return arr[n][tep] = fun(n - 1, (tep + 1) % 2) + fun(n - 2, (tep + 1) % 2);
}