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

    }
}

拿脚本验证下:

相关推荐
Kina_C9 分钟前
LVS负载均衡的十二种核心调度算法
linux·运维·算法·负载均衡·lvs
hansang_IR1 小时前
【题解】[AGC020E] Encoding Subsets
c++·算法·dp
核数聚2 小时前
【赛迪专访核数聚】深耕数据治理,打通数据孤岛夯实 AI 发展根基
大数据·人工智能·算法
小许同学记录成长2 小时前
相对辐射定标技术文档
图像处理·算法
白白白小纯2 小时前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
小羊先生car2 小时前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#
无忧.芙桃3 小时前
数据结构之堆
c语言·数据结构·c++·算法·
Sagittarius_A*3 小时前
[LitCTF2026] lit_xor_two_story
python·算法·密码学
小蒋学算法3 小时前
算法-删除元素后最大固定点数目-典型最长增长序列算法
数据结构·算法
RD_daoyi3 小时前
Google偷偷给AI引用加了五个新功能:内联引用、悬停预览、品牌展示……但点击率真的回来了吗?
大数据·网络·人工智能·算法·安全·搜索引擎