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

    }
}

拿脚本验证下:

相关推荐
怪奇云呼军32 分钟前
教育试听预约后没到课,闪电智能 Voice Agent 如何做分层回访?
人工智能·python·算法·云计算·音视频
Nil20838 分钟前
leetcode 416分割等和子集
算法·leetcode·职场和发展
云淡风轻~窗明几净40 分钟前
角谷猜想的进展之三 移动了群山/沸騰的群山 2026-10-10
人工智能·算法·dubbo·图论
ForeverYang20151 小时前
一种无需训练缺陷数据生成算法--O2MAG
人工智能·深度学习·算法
xuxigifxfh2 小时前
牛客:HJ170 01序列
java·开发语言·算法·华为机考
朝朝辞暮i2 小时前
C++ 第 33 课:事件循环——spin() 到底在干什么
开发语言·c++·算法
殷色玫瑰2 小时前
AVL树:从平衡因子到四种旋转的完整实现
c语言·数据结构·c++·算法·visualstudio
jidaowansui3 小时前
B3643 图的存储———>数组模拟邻接矩阵
算法
y1su3 小时前
Leetcode 二分模板
java·数据结构·算法·leetcode·排序算法
youshi66683 小时前
平衡车——PID算法优化
stm32·单片机·嵌入式硬件·算法·pid·平衡车