一隻青蛙想跳階梯,它每次可能跳一個階梯也可能跳兩個階梯,請問這隻青蛙跳到N個階梯跳了多少次?spa
答案:code
public static void Main(string[] args) { Console.WriteLine("Please write ladder number: "); int n = Convert.ToInt32(Console.ReadLine()); ComputeLeap(n); Console.WriteLine("total=" + total.ToString()); Console.ReadLine(); } public static int total = 0; public static void doComputeLeap(int n) { if (n == 0) { ++total; return; } else if (n == 1) { doComputeLeap(n - 1); } else if (n >= 2) { doComputeLeap(n - 1); doComputeLeap(n - 2); } } public static void ComputeLeap(int n) { if (n == 0) { return; } else { doComputeLeap(n); } }