[練習] 求斐波那契數列第n個數

using System;
namespace 斐波那契
{
    class Program
    {
        static void Main(string[] args)
        {
            UInt32 n;
            Console.WriteLine("輸入求結果的個數值:");
            n = Convert.ToUInt32(Console.ReadLine());
            if (n == 1)
            {
                Console.WriteLine("第一個數爲1");//第一個數沒有不符合以下算法
            }
            else
            {
                int i, a = 0, b = 1, result = 1;
                for (i = 1; i < n; i++)//只有小於才能正確實現
                {
                    result = a + b;
                    a = b;
                    b = result;
                }
                Console.WriteLine("{0}個數爲{1}", n, result);

            }
            Console.ReadKey();
        }
    }
}

 

 

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