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);

    }
}

拿脚本验证下:

相关推荐
EAI-Robotics14 小时前
机器人操作鲁棒性:当机械手遇上真实世界的不确定性
人工智能·算法·机器人
Tairitsu_H15 小时前
[LC优选算法#17] 链表 | 合并 K 个升序链表 | K个⼀组翻转链表
数据结构·算法·链表
_olone15 小时前
Luogu P2704 [NOI2001] 炮兵阵地
c++·算法·状压dp
kebidaixu16 小时前
两轮BMS 防打火策略详解
算法
wabs66617 小时前
关于动态规划【力扣1035.不相交的线和53.最大子数组和的思考】
算法·leetcode·动态规划
退休倒计时17 小时前
【每日一题】LeetCode 199. 二叉树的右视图 TypeScript
算法·leetcode·typescript
可编程芯片开发17 小时前
通过MATLAB实现PID控制器,积分分离控制器以及滑模控制器
算法
kebidaixu17 小时前
两轮BMS 短路保护策略详解
算法
zwenqiyu17 小时前
非线性字符串数据结构串讲
数据结构·c++·学习·算法
z小猫不吃鱼17 小时前
03 Optimal Brain Surgeon 详解:Hessian 剪枝为什么有效?
算法·机器学习·剪枝