递归函数

You might wonder how we can implement a function that calls itself.
  • 您可能想知道我们如何实现一个自我调用的函数
  • might 或,威力,强权
  • wonder奇迹
  • might wonder可能想知道
  • implement实施,实行,实现,执行
  • call呼叫,叫,称,称为,召唤
  • itself本身,自身
The trick is that each time a recursive function calls itself, it reduces the given problem into subproblems.
  • 诀窍在于,每次递归函数调用自身时,都会将给定的问题简化为子问题。
  • trick 招,哄骗,特技,诡计,骗术,戏法
  • each每,各,各自
  • time时间,时候
  • recursive递归的
  • reduce减少,渐低,减低,减缩,还原
  • problem问题,难题
  • into进入,直到
  • subproblem子问题
The recursion call continues until it reaches a point where the subproblem can be solved without further recursion.
  • 递归调用将一直运行,直到达到无需进一步递归即可解决子问题的程度。
  • recursion递归
  • continues继续,持续,延续,接续
  • until直到,为止
  • reach达到,到达
  • point点
  • solve解决
  • solved解决了
  • without没有,在外
  • further进一步,且
A recursive function should have the following properties so that it does not result in an infinite loop:
  • 递归函数应具有以下属性,以免导致无限循环:
  • should应该,应当
  • following以下,下列,之后,以后,跟随
  • property属性,性能,特性,性质
  • result结果,成果
  • infinite无限的
  • loop循环
1.A simple 'base case '(or cases) — a terminating scenario that does not use recursion to produce an answer.
  • 一个简单的“基础案例”(或多个案例)—一种不使用递归来产生答案的终止方案。
  • simple简单,简易,单纯
  • base基础,基地
  • case案件,外壳,例
  • base case基础情况
  • terminating终止
  • scenario情景,脚本
  • produce产生,生产,产品
  • answer回答,应答,答复
2.A set of rules, also known as recurrence relation that reduces all other cases towards the base case.
  • 一组规则,也称为递归关系,可将所有其他情况减少为基本情况。
  • set组
  • rule规则,统治
  • also也,还,还有,又,并
  • as如,如同,既然,既,既是
  • recurrence复发,循环,再起
  • relation关系,关联,联系,叙述
  • reduces减少,降低,缩小,减低,缩减
  • case案件,例子
  • towards向,朝
Note that there could be multiple places where the function may call itself.
  • 请注意,函数可能在多个地方调用自身。
  • Note注意
  • clould 可以
  • multiple多种,众多
  • place地点,地方,位置
  • may可能,可以
Let's start with a simple programming problem:
  • 让我们从一个简单的编程问题开始
  • start开始
  • with与,以,同,跟,将,跟...一起
  • simple简单,简易,朴实
  • problen问题
  • 以相反的顺序打印字符串。
  • reverse逆转,相反
  • order指令,订购,命令,顺序,秩序
You can easily solve this problem iteratively, i.e. looping through the string starting from its last character.
  • 您可以轻松地迭代解决此问题,即从字符串的最后一个字符开始遍历字符串。
  • easily容易,轻易
  • solve解决
  • problem问题,困难
  • iterative反复的
  • looping循环播放
  • through通过
  • string串
  • start开始
  • from从,从中
  • its它的
  • last持续,最后,上,末
  • character字符,字,人物,性格,性质
But how about solving it recursively?
  • 但是如何递归解决呢?
First, we can define the desired function as printReverse(str[0...n-1]), where str[0] represents the first character in the string. Then we can accomplish the given task in two steps:
  • 首先,我们可以将所需函数定义为“ printReverse(str [0 ... n-1])”,其中“ str [0]”表示字符串中的第一个字符。 然后,我们可以分两步完成给定的任务:
  • define定义
  • desired想要的,欲望,愿望
  • where哪里
  • represent代表,表示
  • character字符,字
  • string串
  • accomplish完成,做成,作为
  • task任务,工作
  • step步,步骤
  • steps脚步
    '1.printReverse(str[1...n-1])': print the substring 'str[1...n-1]' in reverse order.
    2.'print(str[0])': print the first character in the string.
Notice that we call the function itself in the first step, which by definition makes the function recursive.
  • 注意,我们在第一步中调用了函数本身,根据定义,这使函数具有递归性。
  • notice注意
  • call呼叫,叫,称,称为,召唤
  • itself他自己
  • first第一
  • which哪一个
  • definition定义
  • make使,使得,制造,让
Next, you will find an exercise that is slightly different from the above example. You should try to solve it using recursion.
  • 接下来,您将找到与上述示例稍有不同的练习。 您应该尝试使用递归来解决它。
  • exercise行使,练习
  • slightly略,丝毫
  • different不同,异
  • above以上,上,在...之上
  • example例,例子
  • try尝试,试,试图
  • solve解决
Note: For this exercise, we also provide a detailed solution in this Explore chapter.
  • 注意:对于本练习,我们还将在“探索”一章中提供详细的解决方案。
  • provide提供,规定
  • detailed详细,详尽
  • solution解,解答
  • explore探索
  • chapter章,章节

❤️有则改之,无则加勉。如有错误、建议、疑问,评论或联系飞沙QQ:2602629646
❤️本文来自作者:MrFlySand,转载请注明原文链接:https://www.cnblogs.com/MrFlySand/p/13589503.html

原文地址:https://www.cnblogs.com/MrFlySand/p/13589503.html