一只青蛙想跳阶梯,它每次可能跳一个阶梯也可能跳两个阶梯,请问这只青蛙跳到N个阶梯跳了多少次?

答案:

       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);
            }
        }

 

posted on 2013-06-21 11:14  冬栀叶  阅读(248)  评论(0)    收藏  举报