【算法】用递归的方法求斐波那契数列2021-11-15

PHP斐波那契数列的运算 参加了原力计划…暂时想不出写什么文章好,看到斐波那契数列的实例似乎有点少,那就通过这个例子来讲一讲这个递归与迭代的一些介绍吧。 首先引用百科的一段介绍: 斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,在数学上,斐波那契数列以如下被以递推的方法定义:F(1... 阅读详情

今天来学算法,怎么求斐波那契数列。
首先认识一下斐波那契数列。假设一对小兔子需要一月长大然后获得繁殖能力,之后每月生一对小兔子,每月共有多少只兔子?
请添加图片描述
抽象为数学函数:请添加图片描述
我们用代码去实现这个函数,注意这里在i>2的情况下调用了自身,然后自身还可以继续调用自身,直到计算出结果。

 int Fbi(int i)
 {
 	if(i<2)
 	    return i==0?0:1;
 	else if(i>=2)
 	    return Fbi(i-1)+Fbi(i-2);
 }

在main函数里去打印前40位数

int main(int argc, char** argv) {
	int i=0;
	for(i=0;i<40;i++)
	printf("%d\n",Fbi(i))  ;
	return 0;
}

执行结果:
请添加图片描述递归的定义:把一个直接调用自己或通过一些列语句间接调用自己的函数,称作递归函数。
每个递归函数必须至少有一个条件,满足时递归不再进行,即不再引用自身而是返回值退出。
————————————————————————

day4,今天也没少学东西。
每日名言:
在一秒钟内看到本质的人和花半辈子也看不清一件事本质的人,自然是不一样的命运。——《教父》
The man who see the essence within one second certainly has different destiny with the man who can’t see the essence of an event for a half lifetime.(翻译略难)

斐波那契数列两种算法的时间复杂度 这是2018王道数据结构考研复习指导的第一章思维拓展的题目。 关于斐波那契数列的简介:   斐波那契数列,又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应... 阅读详情

相关推荐

斐波那契数列

高考中的斐波那契数列的相关知识

静雅斋数学 1007

【Android-案例】斐波那契数列的实现,递归算法和多线程编程UI交互

斐波那契数列

weixin_44002043的博客 643

C++用递归实现斐波那契数列

【题目描述】 菲波那契数列是指这样的数列: 数列的第一个和第二个数都为1,接下来每个数都等于前面2个数之和。 给出一个正整数a,要求菲波那契数列中第a个数是多少。 【输入】 第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1≤a≤20)。 【输出】 输出有n行,每行输出对应一个输入。输出应是一个正整数,为菲波那契数列中第a个数的大小。 ...

1839

斐波那契数列和斐波那契数

如何使用java求斐波那契数列和斐波那契数

qq_53317005的博客 1042

嵌套交叉验证的‘双循环’哲学:为什么你的模型评估总是不准?

本文深入探讨了嵌套交叉验证(Nested Cross-Validation)在机器学习模型评估中的关键作用,揭示了传统交叉验证方法存在的偏差问题。通过双重循环设计,嵌套交叉验证有效隔离数据,提供无偏评估,显著提升模型在真实场景中的性能预测准确性。文章还提供了工程实践中的优化策略和量化分析,帮助开发者在计算成本与评估精度之间找到平衡。

b3n4m5q6w7的博客 225

从零开始掌握Python机器学习:七步教程 基础篇

前言 「开始」往往是最难的,尤其是当选择太多的时候,一个人往往很难下定决定做出选择。本教程的目的是帮助几乎没有 Python 机器学习背景的新手成长为知识渊博的实践者,而且这个过程中仅需要使用免费的材料和资源即可。 第一步:基本 Python 技能 如果我们打算利用 Python 来执行机器学习,那么对 Python 有一些基本的了解就是至关重要的。 首先,你需要安装 Python。因为我们后...

hcgx2018的博客 1613

C语言:用递归函数求Fibonacci数列

【代码】C语言:用递归函数求Fibonacci数列。

weixin_73011353的博客 3644

Demo2Reward:用人类演示动态优化VLM奖励模型提示

多模态大模型(VLM)的奖励建模正面临核心瓶颈——静态提示导致语义鸿沟与泛化失效。其本质是将人类判断标准固化为不可更新的文本模板,使奖励信号脱离真实任务意图。Demo2Reward提出范式级改进:以结构化人类演示为元信号,驱动提示权重连续优化,实现从‘结果打分’到‘过程理解’的跃迁。该方法不修改VLM主干,却显著提升RLHF收敛效率与OOD鲁棒性,在医疗、工业质检等场景验证了跨任务迁移与热修复能力。它重新定义了提示工程的技术价值:不是前端微调,而是奖励系统的认知校准基础设施。

weixin_34302798的博客 408

输入n,求斐波那契数列第n项。斐波那契数列的第0项为0,第一项为1,之后的项依次为前两项之和。

思路一: 递归 要求第n项,首先就要求出第n-1,n-2项,依次类推。递归的出口就是第0项为0,第1项为1.使用递归虽然代码很简单,但如果n大一点的话,所产生的运行时开销会非常大,这样做的后果就是效率非常低下。 思路二: 非递归 使用非递归的话,效率会很高。设置两个三个变量分别表示当前值和前两项的值,使用循环求解。(本例中,只使用两个变量表示这些值) #include<stdio.h

火锅——博客 1万+

C#上位机案例:西门子S7协议通信+实时曲线显示(WinForm+ZedGraph)

创建/// PLC数据模型(对应DB1块)/// 采集时间(用于曲线的X轴)set;/// 温度值(℃)set;/// 压力值(MPa)set;/// PLC通信状态set;西门子S7协议通信和实时曲线显示,也是工控现场最常见的场景之一。整个开发过程的关键不在于“写出代码”,而在于“写出符合工控场景的稳定代码”——比如解耦通信逻辑与界面逻辑、处理跨线程问题、限制数据点数量、释放资源等。新手在学习时,建议先跟着步骤搭建好环境,跑通基础功能,再逐步尝试进阶优化。需要注意的是,工控上位机的核心是。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1342

Ceph入门到精通-存储桶通知

这 通知还可以根据前缀的匹配项过滤掉事件,并且 (1) 键的后缀,(2) 附加到对象的元数据属性, 或 (3) 对象标签。在此模式下,操作仅在以下之后确认(确认) 通知将发送到主题的已配置终端节点。这意味着 通知的往返时间(发送 通知主题的终端节点以及接收 确认)被添加到操作本身的延迟中。要更新主题,请使用用于创建主题的相同命令(但在 更新时,使用现有主题的名称和不同的终端节点值)。端口:对于未加密的连接,默认为 5672,对于未加密的连接,默认为 5671 SSL 加密连接。(默认情况下,这是“假的”。

隔壁老瓦的专栏 1131

裴波那契数列

public class Test9 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int k = sc.nextInt(); System....

健健 1154

vivo X200 Ultra前瞻系列(1):重塑影像旗舰标准

在色彩风格化、视频专业度上实现了安卓阵营的突破性进步。其2亿像素长焦、双LYT-818传感器及5轴防抖技术,对追求个性化创作的用户具有强吸引力。若真机表现能兑现硬件承诺,X200 Ultra有望重新定义手机影像的“专业”标准。然而,其厚重设计、价格门槛及生态适配问题,仍需市场检验。vivo X200 Ultra作为2025年上半年影像旗舰,以“全焦段夜视仪+电影级视频”为核心卖点,主打。,对标小米15 Ultra、OPPO Find X8 Ultra等竞品。,顶配版(24GB+2TB)逼近万元。

专注:太空算力、AI infra、异构调度、大模型成本工程 964

数据结构 | 斐波那契数列时间复杂度算法3种

/*前边两个为一种做法*/ /*后边有另外的做法(差分方程以及利用矩阵去做)*/ 第一种做法 这是王道数据结构考研复习指导的第一章思维拓展的题目。 关于斐波那契数列的简介:   斐波那契数列,又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会...

"You are worthy! You can do it!" 1万+

LVS 负载均衡面试题及参考答案

什么是 LVS 负载均衡?它的主要作用是什么? LVS(Linux Virtual Server)是一个基于 Linux 内核的开源负载均衡项目。它通过将请求流量按照一定的算法分发到后端的多个真实服务器(Real Server)上,从而实现负载均衡的功能。 从原理上来说,LVS 工作在网络层和传输层之间。当客户端发起请求时,请求首先到达 LVS 服务器,LVS 会根据预先设定的规则和算法,比如轮询(Round - Robin)、加权轮询(Weighted Round - Robin)、最少连

大模型大数据攻城狮的专栏 1469

uniapp | 基于高德地图实现位置选择功能(安卓端)

在uni-app项目中实现地图位置选择功能的解决方案。由于uni.chooseLocation功能受限,采用高德地图API自主开发。系统由父组件poiMap.vue(负责展示和跳转)和子组件poiBox.vue(核心地图逻辑)构成,通过父子组件通信传递位置数据。关键技术包括:1)使用AMapLoader动态加载地图SDK;2)集成PositionPicker实现拖拽选点;3)异步加载AMapUI插件;4)通过事件监听获取位置详情(地址/经纬度)。

大胆尝试,小心实践 732

斐波那契数列的三种时间复杂度

七八那也数列

m0_57741101的博客 4354

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

linux求斐波那契数列前10项,linux求斐波那契数列的前10项以及总和.

C语言的函数问题求斐波那契前四十个数,斐波那契数列指的是这样一个数列:0、1、1、2、3、5、8、13、21、……在数学/>#include//thenestfunctionusedtocalculatethe&nbsVB:斐波那契数列第一项是1,第二项是1,用递归算法编写一个程序,求数列前N项的和添加一个文本框输入前N项的N值,再添加一个命令按钮即可PrivateFun...

weixin_29019241的博客 3780

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

求fibonacci数列第n项java_求斐波那契数列的第n项

问题描述:斐波那契数列是这样的一个数列,1,1,2,3,5,8,..,即前两项都是1,后面每一项都是其前面两项的和。现在要你求出该数列的第n项。分析:该问题是一个经典的数列问题,相信大家在很多语言的教科书上都碰到过这个练习题目。这里我给大家总结了三种经典解法,并对这三个方法进行了对比。解法一:递归算法。很多教科书上都用这个题作为函数递归知识点讲解的例题,我们可以将每一个项的求法表达为这样一个式子:...

weixin_39673471的博客 734

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

斐波那契数列 python 列表_Python_斐波那契数列

斐波那契数列斐波那契数列有称为黄金分割数列。在数学上,斐波那契数列可以以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n>=2,n属于正整数)。简单的归结斐波那契数列为:第1个数值为0,第2个数值为1,之后的每个数值是位于它之前的两个数值之和。代码实现每隔一秒打印一个“斐波那契”数列数字,并且将数列保存到指定的文件中。1 #!/user/bin/env pyth...

weixin_39660408的博客 1390

练习2:斐波那契数列数列通过C语言实现

解决c语言完成斐波那契数列问题

2301_80127108的博客 955

从两种斐波那契数的实现认识递归算法

一、递归特性的不足

C-Jonn 995

求斐波那契数列的第n项

问题描述:斐波那契数列是这样的一个数列,1,1,2,3,5,8,..,即前两项都是1,后面每一项都是其前面两项的和。 现在要你求出该数列的第n项。 分析:该问题是一个经典的数列问题,相信大家在很多语言的教科书上都碰到过这个练习题目。这里我给大家总结了三种经典解法,并对这三个方法进行了对比。 解法一:递归算法。很多教科书上都用这个题作为函数递归知识...

weixin_30741653的博客 2628
上一篇: 【数据结构】栈(上)2021-11-14
下一篇: c++ 类的显式转换、隐式转换、可调用对象
Sweler*19
博客等级 码龄7年 65粉丝 56原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值