阶梯递归速算

题目:
小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!

站在台阶前,他突然又想着一个问题:

如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?

 

个人分析:
/* 不排序时共10种走法
*  1次2步的 39-2+1 有38次一部 也就是2次步的可以换38次
*    <偶次倍2步的,直接忽略>XXXXXXX 2次两部的 去掉4个 37
*    3 6 36    3步连着有36次 2
*    5 10 34   
*     7 14 39-14+7=32
*    9 18 39-18+9==30
*    11    22 39-22+11=28
*    13
*    15
*    17
*    19    共20次 2次步最多的一次
*/

 

正确代码:

public class TAIJIE {
	static int count = 0;
	public static void test(int jieti,int step){
		if(jieti<0) return;
		if(jieti==0){
			if(step%2==0) count++;
		}
		test(jieti-1,step++);
		test(jieti-2,step++);
	}
	
	public static void main(String[] args) {
		test(39,0);
		System.out.println(count);
	}
}

  

 

posted @ 2016-08-09 09:45  虫虫吖  阅读(454)  评论(1编辑  收藏  举报