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

    }
}

拿脚本验证下:

相关推荐
小指纹几秒前
每日一题--Tokitsukaze and Colorful Chessboard【二分】
数据结构·c++·算法
铭哥的编程日记5 分钟前
小企鹅装石头(栈模拟题)
算法
汉堡go7 分钟前
SLAM数学基础1
人工智能·算法·机器学习
qzhqbb7 分钟前
不可检测水印
人工智能·算法
快敲啊死鬼8 分钟前
机试day5
算法·华为od·华为
8Qi810 分钟前
LeetCode热题100--189
c语言·数据结构·c++·算法·leetcode
灰色小旋风10 分钟前
力扣第八题C++ 字符串转换整数
c++·算法·leetcode
@––––––12 分钟前
力扣hot100—系列9—图论
算法·leetcode·图论
pp起床14 分钟前
图论 | part01
算法·深度优先·图论
luckycoding14 分钟前
3676. 碗子数组的数目
算法·游戏·深度优先