用递归法计算斐波那契数列的第n项

递归求斐波那契数列第n项 详解 C语言入门 欢迎关注笔者,你的支持是持续更博的最大动力 目录问题描述思路代码相关内容其他 问题描述 递归求斐波那契数列第n项的值 思路 斐波那契数列(Fibonacci sequence): 1,1,2,3,5,8… 规律: 除第1、2项,从第3项起,值为前两项数之和 如: 公式: F(1) = 1,F(2) = 1, F(n) = F(n-1)+ F(n-2)    (n ≥3, n∈N*) 代码 将上述公式写成代码: //定义斐波那契Fibonacci函数 int Fib(int n). 阅读详情

include <stdio.h>

int F(int num)
{
        if(num==0)
        {
                return 0;
        }
        if(num==2||num==1)
        {
                return 1;
        }

        return F(num-1)+F(num-2);
}

int main()
{
        int num,n,i;
        printf("input you want the number\n");
        scanf("%d",&num);
        for(i=1;i<=num;i++)
        {
                printf("%3d",F(i));
        }

        return 0;
                                                      1,1           Top
 

面试算法(八)斐波那契数列 1、写一个函数,输入n,求斐波那契数列的第n项。 阅读详情

相关推荐

斐波那契数列的多种算法,迭代法+递归法+动态规划+矩阵幂

现在要求输入一个整数n,请你输出斐波那契数列的第n项。#include <stdio.h> #include <stdlib.h> /** 现在要求输入一个整数n,请你输出斐波那契数列的第n项。 n<=39 */ int Fibonacci(int n); int main() { int s = 0; s = Fibonacci(39); printf("result =

xiaoyaoyoufeng的博客 1291

编写递归函数,求斐波那契数列第n项

要求:编写递归函数int f(int n),计算如下公式:定义main函数输入n,调用f函数进行计算,在main函数中输出计算结果。【样例输入】10【样例输出】89int fun(int n)

m0_73509997的博客 5329

【Python 基础】求n的阶乘(递归法) || 动态规划 求 n项斐波那契数列(前n项) || 递归法 求 “斐波那契数列” 的 第n项

Python:计算阶乘、计算斐波那契数列(递归法、动态规划)。【Python 基础】求n的阶乘(递归法) || 动态规划 求 n项斐波那契数列(前n项) || 递归法 求 “斐波那契数列” 的 第n项

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 1173

C语言学习记录——用递归思想求第n个斐波那契数,函数递归

C语言学习记录——用递归思想求第n个斐波那契数,函数递归

li13437542099的博客 5431

使用PaddleOCR识别查验平台验证码

PaddleOCR识别国税查验平台验证码

m0_63932300的博客 1269

Shader之——花草动画shader

今天 ,接到了个需求,花草的动画。谈到花草动画,立马就想到顶点动画,让顶点动起来就可以实现了。但是有个问题,所有的草都往一个方向运动,这个就麻烦了。因为在shader里没有随机这种概念,提到最多的是用一张噪声图来模拟。在网上搜索了一下,发现有更好的办法,就是用每个顶点到一个点的距离来控制动画的开始,毕竟每个顶点到同一个点的距离很少是一样的。 float dis = distance ...

baicaishisan的博客 2184

用递归函数,求第n项斐波那契数列

首先,我们得先了解斐波那契数列。 斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardo Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列: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*) 其次,我们可以看到,当n大于2时F(n)是由前面两个相加所得,所以我们可

weixin_62524570的博客 5980

中国大学MOOC-翁恺-C语言程序设计习题集-解答汇总

中国大学MOOC-翁恺-C语言程序设计习题集 PAT 习题集 02-0. 整数四则运算(10) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 乔林(清华大学) 本题要求编写程序,计算2个正整数的和、差、积、商并输出。题目保证输入和输出全部在整型范围内。 输入格式: 输入在一行中给出2...

weixin_30691871的博客 3万+

递归与迭代 | 求斐波那契数列第n项值的四种算法

前言: 昨儿晚上三点多睡不着,不知道胡思乱想了些啥,好不容易睡着了又做了些稀奇古怪的梦。考研还是继续,真难。 这一篇博客记录一下求斐波那契数列第n项值得几种方法,用到了递归和迭代的方法,所以首先我们来区分一下递归和迭代,再来记录几种方法。 递归和迭代 联系 递归与迭代都是循环的一种。 区别 1、程序结构不同 递归是重复调用函数自身的循环。 迭代是函数内部代码的循环。 注意:迭代和普通循环的区别:迭代时,循环代码中参与计算的变量也是要返回的结果,当前保存的结果作为下一次循环计算的初始值。 2、算法结束方式

weixin_46105448的博客 5398

QML 地图修改插件源码(三),Map在Plugin中设置加载地图类型

常用的地图种类分为交通图,地形图,卫星图等等,在QML的Map(以OSM地图为例)中提供activeMapType属性用于读取当前显示的地图类型(注意:该属性为只读属性,不能用于赋值),QML中地图的加载寻找根据瓦片的文件名进行查找,例如名为"osm_100-l-5-2-2-3.png"的瓦片,其中5代表了该地图的类型

zjgo007的专栏 3164

2D激光slam四种算法建图效果对比

2d激光slam四种算法建图比较

m0_73791170的博客 8448

c语言递归求斐波那契第100项,用递归法计算斐波那契数列的第n项

用递归方法计算斐波那契数列的第n项的代码如下:#includeintFibonacci(intn){if(n==1||n==2)//递归结束的条件,求前两项return1;elsereturnFibonacci(n-1)+Fibonacci(n-2);//如果是求其它项,先要求出它前面两项,然后做和。}intmain(){intn;printf("please...

weixin_29986051的博客 1933

【读代码】SQLBot:开源自然语言转SQL智能助手原理与实践

SQLBot是 DataEase 团队开源的自然语言转 SQL 智能助手,致力于让非技术用户也能通过自然语言与数据库对话,自动生成 SQL 查询,实现自助数据分析、智能BI问答、报表生成等场景。SQLBot 结合了大语言模型(LLM)、数据库元数据解析、SQL解析与执行等多项技术,具备强大的多数据库适配能力和灵活的扩展性。降低数据分析门槛,让业务人员无需掌握SQL即可获取数据洞察支持多种数据库类型,适应企业多样化数据环境提供可扩展的API和插件机制,便于集成到各类BI、数据平台和业务系统。

kakaZhui的博客 829

用递推法和递归法计算斐波那契(Fibonacci)数列的第n项

斐波那契数列 递推法 编写程序,用递推法计算斐波那契(Fibonacci)数列的第n项。 求解思路:斐波那契(Fibonacci)数列为0,1,1,2,3,5,8,13,……,即: F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2),当n>1时。 用递推法编写的程序为: #include<stdio.h> int Fib(int n) { int f0=0,f1=1,f,i; if(n==0||n==1) { return n;

qq_44293330的博客 1万+

从零开始学习CANoe(十六)—— Graphics

Graphics是一种以时间轴为横坐标,符号值为纵坐标的显示分析方法,相对于Trace来说,更直观的观察出信号的跳变

蚂蚁小兵 9193

【QT开发教程】QMatrix4x4类详解及实战应用:Qt中的4x4矩阵变换

在计算机图形学和三维图形应用中,矩阵变换是一个核心概念。Qt框架提供了QMatrix4x4类,用于处理4x4矩阵变换,帮助开发者在三维空间中进行复杂的几何计算。本文将深入探讨QMatrix4x4的功能及其在Qt应用中的实际应用。

科技改变人类,技术成就未来 719

php用递归法计算斐波那契数列的第n项

function counNum($num){ if($num==0 || $num==1) return $num; return (counNum($num-1) + counNum($num-2)); } 注意:php计算大于第40位的数的时候就会很困难

jerryyang_2017的博客 2648

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

用递归法计算斐波那契数列的第n项 (1,1,2,3,5,8,13)

public int GetNum(int x) { if (x &lt;= 0) { return 0; } if (x == 1 || x == 2) { return 1; } return GetNum(x - 2) +...

Dream_Island 3万+

深度学习工具包 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")

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

斐波那契数列——C语言

F。

weixin_63292027的博客 2万+

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

剑指Offer 007斐波那契数列

007斐波那契数列 题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(n<=39)(从0开始,第0项为0) 方法一:递归法 思路:判断n的值,进行递归 缺点:进行太多的重复计算,时间成本高,容易超时 代码: public int Fibonacci(int n) { if(n<=0){ return 0; ...

从零学起 136

C/C++ 每日一练:计算斐波那契数列的第 n 项(递归、记忆化、迭代)

因此,斐波那契数列的前几项为:1, 1, 2, 3, 5, 8, 13, 21, 34, ...。斐波那契数列在自然界、艺术和数学中都有广泛的应用,例如兔子繁殖问题、植物生长、黄金分割等,是数学中一种典型的递推数列。与递归相比,迭代法无需栈空间,时间复杂度为 O(n),空间复杂度为 O(1)。记忆化递归是对递归方法的优化。递归法的核心思想是将一个大的问题分解成两个较小的子问题,再继续递归地分解,直到到达递归终止条件。第 n 项 F(n) = F(n-1) + F(n-2),其中 n > 2。

weixin_60461563的博客 3975
上一篇: 用递归思想计算 x 的 N 次方。x 可以是任意浮点数,N 是整数
下一篇: 如何判断字节序
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值