题目:
一个人爬楼梯,一步可以迈一级,二级,三级台阶,如果楼梯有N级,编写程序,输出所有走法。java实现。
不考虑顺序的算法(第几次迈几个台阶)
public class Hello {
private static final int N = 3000; // 总共有N个台阶
private static final int Tai_One = 1; // 一次跨1步台阶
private static final int Tai_Two = 2; // 一次跨2步台阶
private static final int Tai_Three = 3; // 一次跨3步台阶
// 每一种跨步方法单独跨的最大值
private static final int Max_One = N / Tai_One;
private static final int Max_Two = N / Tai_Two;
private static final int Max_Three = N / Tai_Three;
public static void main(String[] args) {
Hello.method();
}
/**
*/
public static void method() {
int count = 0;
long beginT = System.currentTimeMillis();
for (int i = 0; i <= Max_One; i++) {
for (int j = 0; j <= Max_Two - i / Tai_Two; j++) {
for (int k = 0; k <= Max_Three - i / Tai_Three - j / Tai_Three; k++) {
if (i * 1 + j * 2 + k * 3 == N) {
count++;
System.out.println(i + " " + j + " " + k);
}
}
}
}
long endT = System.currentTimeMillis();
System.out.println("total method:" + count + "---耗时:"
+ (endT - beginT));
}
}
考虑迈的顺序(以下代码转自点击打开链接)
考虑顺序的情况一种实现方式是通过“构造树”来实现。另一种实现思路就是典型的斐波那契数列应用。
构造树实现:
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
package com.csdn.question;
import java.util.ArrayList;
import java.util.List;
/**
* Description: 一个人爬楼梯,一步可以迈一级,二级,三级台阶,如果楼梯有N级,编写程序,输出所有走法。java实现。 Thinking:
* 先试探性的走出第一步,在第一步的基础上递归处理剩下的台阶数,直到最后走出一棵树,然后遍历树,打印出路径
*
* @author karl
*/
public class ClimbStairs_ThunderSubject {
public static final int step_1 = 1; // 一步一级
public static final int step_2 = 2; // 一步二级
public static final int step_3 = 3; // 一步三级
private static int total = 0;
/**
* Description: 开始走楼梯,即创建基于根节点root的树
*
* @param root
* 根节点
* @param n
* 楼梯级数
*/
public static void climbStaris_CreateTree(Node root, int n) {
if (n == 1) { // 若台阶数为1,则只能走一步一级
Node step = new Node(step_1);
root.children.add(step);
} else if (n == 2) { // 若台阶数为2,第一步可以走一级,也可以走两阶,用for循环处理
for (int i = 1; i <= 2; i++) {
Node step = new Node(i);
root.children.add(step);
// 走完第一步,剩下的台阶数递归处理
int left = n - i;
climbStaris_CreateTree(step, left);
}
} else if (n >= 3) { // 若台阶数大于等于3,则第一步有三种走法,一级,二级,或三级,for循环处理
for (int i = 1; i <= 3; i++) {
Node step = new Node(i);
root.children.add(step);
// 走完第一步,剩下的台阶数递归处理
int left = n - i;
climbStaris_CreateTree(step, left);
}
}
}
/**
* Description: 打印楼梯的走法,即遍历整个树
*
* @param root
* 需要遍历的父节点
* @param rootPath
* 需要遍历的父节点相对于树根节点的路径
*/
public static void printPath_ergodicTree(Node root, String rootPath) {
// 若传入的父节点即为叶子节点,则直接打印其路径,完毕后回,遍历下一个节点
if (root.children.size() == 0) {
total++;
System.out.println(rootPath);
return;
}
// 若传入的父节点有子节点,则递归遍历其子节点
for (int i = 0; i < root.children.size(); i++) {
Node child = root.children.get(i);
String childPath = rootPath + "-" + String.valueOf(child.val);
// 递归处理其子节点
printPath_ergodicTree(child, childPath);
}
}
public static void main(String[] args) {
Node root = new Node();
climbStaris_CreateTree(root, 20);
printPath_ergodicTree(root, "start");
System.out.println(total);
}
}
/**
* Description: 定义树的节点
*
* @author karl
*/
class Node {
int val;
List<Node> children = new ArrayList<Node>();
Node() {
}
Node(int val) {
this.val = val;
}
}
斐波那契数列实现:
f(n) = 1 (n = 1);
= 2 (n = 2);
= 4 (n = 3);
= f(n-3) + f(n-2) + f(n-1) (n>3);
package com.csdn.question;
import java.util.ArrayList;
import java.util.List;
public class Fibonacci {
private static int total = 0;
public static void main(String[] args) {
goUp(3);
System.out.println(total);
}
public static void goUp(int n) {
List<String> fibList = new ArrayList<String>();
fibonacci(n, fibList);
}
private static void fibonacci(int n, List<String> fibList) {
if (n == 0) {
for (String str : fibList) {
System.out.print(str + " ");
}
total++;
System.out.println();
} else if (n < 0) {
return;
} else {
int num = fibList.size();
fibList.add("1");
fibonacci(n - 1, fibList);
fibList = fibList.subList(0, num);
fibList.add("2");
fibonacci(n - 2, fibList);
fibList = fibList.subList(0, num);
fibList.add("3");
fibonacci(n - 3, fibList);
fibList = fibList.subList(0, num);
}
}
}