第三週C#上機實驗(一)有規律的求和

簡述實驗:一列數的規則如下:112358132134......。求第30位數是多少?

源代碼:

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = 1, n2 = 1,n3 = 0;
            Console.Write("按此規律算數1、1、2、3、5、8....");
            Console.WriteLine();
            for (int i = 3; i <= 30; i++)
            {
                n3 = n1 + n2;
                n1 = n2;
                n2 = n3;

            }
            Console.WriteLine("第30個數是:{0}", n3);
            Console.ReadKey(false);
        }
    }
}


輸出結果:


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