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

    }
}

拿脚本验证下:

相关推荐
半壶清水13 分钟前
[软考网规考点笔记]-操作系统核心知识及历年真题解析
网络·网络协议·算法
Tansmjs27 分钟前
实时数据可视化库
开发语言·c++·算法
WBluuue29 分钟前
Codeforces 1075 Div2(ABC1C2D1D2)
c++·算法
2401_838472511 小时前
C++模拟器开发实践
开发语言·c++·算法
s1hiyu1 小时前
实时控制系统验证
开发语言·c++·算法
daad7771 小时前
V4L2_mipi-csi
算法
2301_765703141 小时前
C++代码复杂度控制
开发语言·c++·算法
m0_708830961 小时前
C++中的享元模式实战
开发语言·c++·算法
naruto_lnq2 小时前
分布式计算C++库
开发语言·c++·算法
m0_706653232 小时前
模板编译期排序算法
开发语言·c++·算法