棧的應用——Fibonacci——斐波那契數列

如果兔子在出生兩個月後,就有了繁殖能力,一堆兔子每個月能生出一對小兔子來。假設所有的兔子都不死,那麼一年後可以繁殖多少個小兔子?

兔子對數:1 ,1,2,3,5,8,13,21,34,55,89,144

遞歸方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fbi
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(Fbi(i));
            }
            Console.ReadKey();
        }
        public static int Fbi(int i)
        {
            if (i < 2)
            {
                return i == 0 ? 0 : 1;
            }
            return Fbi(i - 1) + Fbi(i - 2);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章