利用数组实现斐波那切数列 c# 1025

203 阅读1分钟
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            // 来一个三十长度的数组
            int[] feibo = new int[30];

            // 初始化一号二号位数据
            feibo[0] = 1;
            feibo[1] = 1;

            // 从第三个数据开始,就是前二个数据的和了
            for (int idx = 2; idx < feibo.Length; idx++) { 
                // idx就是索引下标,从2开始(代表了第三个数据)
                // 当指针指向了idx的时候,应该是前两个数的和赋值给我当前下标的位置
                // 当前指针指向的位置 = 前一个位置的值+前二个位置的值
                feibo[idx] = feibo[idx - 1] + feibo[idx - 2];
            }

            // 最终输出结果看看
            foreach(int res in feibo){
            Console.WriteLine(res);
            }

            // 键盘等待
            Console.ReadKey();
            
        }
    }
}