`
viking.liu
  • 浏览: 53659 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

爬台阶问题

阅读更多
package com.viking.dynamic;

/**
 * 
 * @author viking
 * 
 *         有n步台阶,一次只能上1步或者2步,求一共有多少中走法
 * 
 *         f(n)=f(n-1)+f(n-2)
 *         
 *         f(1)=1 f(2)=2
 */
public class Step {
	public static void main(String[] args) {
		int n = 10;
		int s = steps(n, n+"=");
		System.out.println(s);
	}

	public static int steps(int n, String path) {
		if (n == 1) {
			System.out.println(path+"1");
			return 1;
		}
		if (n == 2) {
			System.out.println(path+"1->1");
			System.out.println(path+"2");
			return 2;
		}
		return steps(n - 1,path+"1->") + steps(n - 2,path+"2->");
	}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics