C#:简单算法,求斐波那契数列

在加某个数值群的时候,要求求n位以后的斐波那契数列。

已知,斐波那契数列为1,1,2,3,5,8,13,21,34,55,89,144, ......

即最后1位为前两位之和。所以可以得出算法脚本如下:

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public int num;
    // Start is called before the first frame update
    void Start()
    {

    }

    void CreatNum(int _a, int _b, int _d)
    {
        var _c = _a + _b;
        _a = _b;
        _b = _c;
        _d --;
        Debug.Log(_a + "  " + _b + "  " + _c + "  " + _d);
        if (_d <= 1) Debug.LogError(_c);
        else CreatNum(_a, _b, _d);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
            CreatNum(0, 1, num);

    }
}

拿脚本验证下:

相关推荐
心中有国也有家19 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
绝知此事19 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院20 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
csdn_aspnet20 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
m0_629494731 天前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户1 天前
用队列实现栈
数据结构·算法
做人求其滴1 天前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣
daad7771 天前
记一组无人机IMU传感器数据
算法
计算机安禾1 天前
【c++面向对象编程】第42篇:模板特化与偏特化:为特定类型定制实现
开发语言·c++·算法