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

    }
}

拿脚本验证下:

相关推荐
WBluuue3 分钟前
Codeforces 1087 Div2(ABCDEF)
c++·算法
Yzzz-F28 分钟前
2025 ICPC武汉邀请赛 G [根号分治 容斥原理+DP]
算法
abant232 分钟前
leetcode 114 二叉树变链表
算法·leetcode·链表
tankeven35 分钟前
HJ165 小红的优惠券
c++·算法
先积累问题,再逐次解决1 小时前
快速幂优美算法
算法
XiYang-DING1 小时前
【LeetCode】 225.用队列实现栈
算法·leetcode·职场和发展
花月C2 小时前
线性动态规划(Linear DP)
算法·动态规划·代理模式
hetao17338372 小时前
2025-03-24~04-06 hetao1733837 的刷题记录
c++·算法
_深海凉_2 小时前
LeetCode热题100-环形链表
算法·leetcode·链表