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

    }
}

拿脚本验证下:

相关推荐
专注VB编程开发20年1 天前
图片转矢量图(提取轮廓线条)Potrace:一个基于多边形的位图轮廓矢量化算法(translation)
算法·图片转矢量
小羊失眠啦.1 天前
Rust核心库(core)深度解析:无依赖基石的设计与实践
数据库·算法·rust
Wenhao.1 天前
LeetCode Hot100 每日温度
数据结构·算法·leetcode·golang
吃着火锅x唱着歌1 天前
LeetCode 1679.K和数对的最大数目
算法·leetcode·职场和发展
im_AMBER1 天前
Leetcode 57
笔记·学习·算法·leetcode
im_AMBER1 天前
Leetcode 58 | 附:滑动窗口题单
笔记·学习·算法·leetcode
sin_hielo1 天前
leetcode 2154
算法·leetcode
Sunhen_Qiletian1 天前
YOLO的再进步---YOLOv3算法详解(上)
算法·yolo·计算机视觉
ANYOLY1 天前
Sentinel 限流算法详解
算法·sentinel
No0d1es1 天前
电子学会青少年软件编程(C/C++)六级等级考试真题试卷(2025年9月)
c语言·c++·算法·青少年编程·图形化编程·六级