namespace ConsoleApplication1
{
    //斐波那契数列   1,1,2,3,5,8,13,21......
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetFei(10));                                   
            Console.Read();           
        }

        /// <summary>
        /// 递归获取第n项的值
        /// </summary>
        /// <param name="n">第n项(大于0的正整数)</param>
        /// <returns>第n项的值</returns>
        static int GetFei(int n)
        {           
            if (n > 2)
            {
                double temp = GetFei(n - 1) + GetFei(n - 2);
                if (temp > int.MaxValue)
                {
                    return 0;
                }
                else
                {
                    return (int)temp;
                }
            }
            else
            {
                return n > 0 ? 1 : 0;
            }           
        }
    }
}

posted on 2010-12-09 15:03  Efeng  阅读(1366)  评论(1编辑  收藏  举报