- 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第四章归纳与递回-大叶大学
Ch4-* Example 5. Search x in a1, a2,…,an by Linear Search Sol : Alg. 5 procedure search (i, j, x: integers) if ai = x then location := i else if i = j then location := 0 else search(i+1, j, x) 從ai,ai+1,…aj 中找 x call search(1, n, x) (跳過) Ch4-* Example 6. Search x from a1,a2,…,an by binary search (recursive version). Sol : Alg. 5 procedure binary_search (x , i , j: integers) m := ?(i+j) / 2? if x = am then location := m else if (x am and i m) then binary_search(x, i, m-1) else if (x am and j m) then binary_search(x, m+1, j) else location := 0 call binary_search(x, 1, n) search x from ai, ai+1, …, aj 表示左半邊 ai, ai+1, …, am-1 至少還有一個元素 (跳過) Ch4-* Example 1. Give the value of n!, n?Z+ Sol : Note : n! = n ? (n-1)! Alg. 1 (Recursive Procedure) procedure factorial (n: positive integer) if n = 1 then factorial (n) := 1 else factorial (n) := n ? factorial (n-1) Alg. (Iterative Procedure) procedure iterative_factorial (n : positive integer) x := 1 for i := 1 to n x := i ? x { x = n! } (跳過) Ch4-* ※ iterative alg. 的計算次數通常比 recursive alg.少 ※ Find Fibonacci numbers (Note : f0=0, f1=1, fn=fn-1+fn-2 for n?2) Alg. 7 (Recursive Fibonacci) procedure Fibonacci (n : nonnegative integer) if n = 0 then Fibonacci (0) := 0 else if n = 1 then Fibonacci (1) := 1 else Fibonacci (n) := Fibonacci (n-1)+Fibonacci (n-2) (跳過) Ch4-* Alg.8 (Iterative Fibonacci) procedure iterative_fibonacci (n: nonnegative integer) if n = 0 then y := 0 // y = f0 else begin x := 0 y := 1 // y = f1 for i := 1 to n-1 begin z := x + y x := y y := z end end {y is fn } Exercise : 11, 35 i = 1 i = 2 i = 3 z f2 f3 f4 x f1 f2 f3 y f2 f3 f4 (跳過) * * * * * * * * * * * * *
您可能关注的文档
最近下载
- 发现你的行为风格 -DISC:提高职场沟通效率 完整版.ppt VIP
- 全球数字疗法产业报告.pptx VIP
- 是谁在敲【知识精研】一年级上册音乐粤教花城版.pptx VIP
- 第2课+开放互联——网络协议与标准+课件+2024—2025学年清华大学版(2024)B版初中信息技术七年级上册.pptx VIP
- 一株双歧杆菌发酵条件的研究.pdf VIP
- 小学四年级英语校本课程.doc VIP
- 中学教育学课程.pptx VIP
- GB_T 3880.2-2024一般工业用铝及铝合金板、带材 第 2 部分力学性能.docx VIP
- 中国国家标准 GB/T 24067-2024温室气体 产品碳足迹 量化要求和指南.pdf
- 三级公共营养师基础知识考试刷题(附答案).doc VIP
文档评论(0)