当前位置:网站首页>1143_ SiCp learning notes_ Tree recursion
1143_ SiCp learning notes_ Tree recursion
2022-07-06 13:50:00 【grey_ csdn】
All learning summary :GitHub - GreyZhang/g_SICP: learn SICP and hack lisp.
A typical example of tree recursion is the solution of Fibonacci sequence , The above is a simple definition rule description . This description can be easily converted into a recursive function by code , The last screenshot of the above document is lisp How to implement .
If the replacement model is used for expansion , This is the solution process of Fibonacci sequence . It can be seen from here that , Half the work is actually repetitive . In fact, it can be analyzed from the characteristics of the code itself , Because every recursion calls the function twice .
This is an optimization of Fibonacci sequence solution , In fact, the optimization method is mainly aimed at the iterative part in the previous formula . In previous software design , The state saving and other work of these two parts are actually completely entrusted to the parser . And the improved way , Make a transition between the two maintenance states , In fact, the saving of some temporary information is not completely handled by the parser, but put into memory . The design of software is actually very easy to understand .
This is a test of the above software design . Equivalent functions can also be easily used python To do a writing test .
def fib(n):
if n <= 1:
return n
else:
return fib(n - 1) + fib(n - 2)
def fib_iter(a, b, n):
if n == 0:
return b
else:
return fib_iter(a + b, a, n - 1)
def new_fib(n):
return fib_iter(1, 0, n)
The computational effect of both languages is the same . I also compared the solution method with the first scheme , Solve in the computer fib(100) The process is very long . And the improved way , In fact, it has good execution efficiency , Whether it's lisp still python.
Here is another classic question : The combination of coins . The disassembly of this problem is divided into two steps , Described as the colored part above . About this description , In my own understanding, I should consider this : All the combination methods actually have 2 Kind of , One is that the first kind of coins are not used ; The other is the case of using at least one coin of the first kind . Then the case of using at least one coin of the first kind can be considered as the total amount minus the face value of one coin of the first kind , Any combination of the remaining amounts . such , It's easy to build the following recursive program .
such , Basically finished reading the content of tree recursion . Through the study of this chapter , I still see a useful way of thinking . however , The realization of many laws or methods is actually supported by certain data theory to a great extent . look , Mathematics is also a very important tool and method .
边栏推荐
- 【手撕代码】单例模式及生产者/消费者模式
- 1. C language matrix addition and subtraction method
- 【九阳神功】2022复旦大学应用统计真题+解析
- 7-11 机工士姆斯塔迪奥(PTA程序设计)
- 【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
- [dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- 甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
- 【数据库 三大范式】一看就懂
- 2022 Teddy cup data mining challenge question C idea and post game summary
猜你喜欢
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
Wei Pai: the product is applauded, but why is the sales volume still frustrated
受检异常和非受检异常的区别和理解
FAQs and answers to the imitation Niuke technology blog project (I)
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
.Xmind文件如何上传金山文档共享在线编辑?
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
编写程序,模拟现实生活中的交通信号灯。
Custom RPC project - frequently asked questions and explanations (Registration Center)
随机推荐
hashCode()与equals()之间的关系
MySQL lock summary (comprehensive and concise + graphic explanation)
5. Download and use of MSDN
This time, thoroughly understand the MySQL index
Using spacedesk to realize any device in the LAN as a computer expansion screen
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
2. C language matrix multiplication
Write a program to simulate the traffic lights in real life.
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
Beautified table style
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
实验七 常用类的使用(修正帖)
Wechat applet
仿牛客技术博客项目常见问题及解答(一)
7-7 7003 组合锁(PTA程序设计)
Reinforcement learning series (I): basic principles and concepts
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
[中国近代史] 第六章测验
A piece of music composed by buzzer (Chengdu)
【九阳神功】2020复旦大学应用统计真题+解析