分析一波
典型的斐波那契数列应用。
分析:当 n = 1 时,只有一种跳法;当 n = 2 时,有两种;
当 n > 2 时,
如果第一次跳 1 级,则跳法总数 = F(n-1):后面剩下的 n - 1 级台阶的跳法总数;
如果第一次跳 2 级,则跳法总数 = F(n-2):后面剩下的 n - 2 级台阶的跳法总数;因此 n 级台阶的不同跳法的总数:F(n) = F(n-1) + F(n-2)。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
/**
* 题意:
*/
public class Main {
private static int arr[] = new int[33];
public static void FibonacciPlus() {
arr[1] = 1;
arr[2] = 2;
for (int i = 3; i <= arr.length; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
}
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n;
FibonacciPlus();
while (in.nextToken() != StreamTokenizer.TT_EOF) {
n = (int) in.nval;
out.println(arr[n]);
}
out.flush();
}
}