Scratch3.0----递归函数(2)

网友投稿 2019-06-21 11:25

递归函数

欢迎来到我的订阅号!天波易谢,寸暑难留。上次我们认识了“递归函数”,这是一个很重要的内容,今天我们在做几个练习巩固一下。

1.  斐波那契数列

“斐波那契数列”是由一位意大利的数学家提出来的,指的是一列数字。前两项为1,从第三项开始每一项等于前两项之和。如1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,                   28657,46368........ 这就是“斐波那契数列”,是斐波那契受兔子繁殖的启发发明的,也称为黄金分割数列。

我们先来分析一下,这个数列前两项都为1,那么我们如果选中第二项/第一项,输出1就可以结束了。如果我们选中第三项,我们是不是得要推导出它前面的两项是什么,然后加在一起就是我们的第三项,我们来看一下代码。

https://cdn.china-scratch.com/timg/190623/1125154b1-0.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图1.1 代码

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125154M5-5.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图1.2 运行结果

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

我们可以按照(图1.3)一样,把实参带入推到出来,我们看看结果对不对。

https://cdn.china-scratch.com/timg/190623/1125154U2-10.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图1.3

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125162326-15.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图1.4

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/11251M044-20.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图1.5

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/11251SD1-25.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图1.6

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

2.  我们在用递归做一个求和的案例,求1+2+3+4+...+99+100的和。

https://cdn.china-scratch.com/timg/190623/1125191214-30.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图2.1

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125191C0-35.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

图2.2

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

https://cdn.china-scratch.com/timg/190623/1125152557-1.jpg

这两个案例要好好理解啊,尤其是第一个。

--end--

声明:本文章由网友投稿作为教育分享用途,如有侵权原作者可通过邮件及时和我们联系删除:freemanzk@qq.com