[go: up one dir, main page]

CN111767522B - Recursive algorithm implementation method and device and electronic equipment - Google Patents

Recursive algorithm implementation method and device and electronic equipment Download PDF

Info

Publication number
CN111767522B
CN111767522B CN202010608367.7A CN202010608367A CN111767522B CN 111767522 B CN111767522 B CN 111767522B CN 202010608367 A CN202010608367 A CN 202010608367A CN 111767522 B CN111767522 B CN 111767522B
Authority
CN
China
Prior art keywords
recursive algorithm
execution
recursive
algorithm
last
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010608367.7A
Other languages
Chinese (zh)
Other versions
CN111767522A (en
Inventor
陈舜雨
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Hikvision System Technology Co Ltd
Original Assignee
Hangzhou Hikvision System Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hangzhou Hikvision System Technology Co Ltd filed Critical Hangzhou Hikvision System Technology Co Ltd
Priority to CN202010608367.7A priority Critical patent/CN111767522B/en
Publication of CN111767522A publication Critical patent/CN111767522A/en
Application granted granted Critical
Publication of CN111767522B publication Critical patent/CN111767522B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F17/00Digital computing or data processing equipment or methods, specially adapted for specific functions
    • G06F17/10Complex mathematical operations
    • G06F17/18Complex mathematical operations for evaluating statistical data, e.g. average values, frequency distributions, probability functions, regression analysis

Landscapes

  • Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Optimization (AREA)
  • Pure & Applied Mathematics (AREA)
  • Theoretical Computer Science (AREA)
  • Mathematical Physics (AREA)
  • Computational Mathematics (AREA)
  • Mathematical Analysis (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Operations Research (AREA)
  • Probability & Statistics with Applications (AREA)
  • Bioinformatics & Cheminformatics (AREA)
  • Algebra (AREA)
  • Evolutionary Biology (AREA)
  • Databases & Information Systems (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Bioinformatics & Computational Biology (AREA)
  • Management, Administration, Business Operations System, And Electronic Commerce (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The embodiment of the invention provides a recursive algorithm realization method and device and electronic equipment. The method comprises the following steps: starting to execute a recursion algorithm aiming at a current variable, wherein an initial value of the current variable is a target variable; in the process of executing the recursive algorithm, recording the execution progress of the recursive algorithm executed this time when the executed step is a recursive step, wherein the recursive step is a step requiring to call the recursive algorithm; in executing the recursive algorithm, each time a result of executing the recursive algorithm for a current variable is obtained, the execution of the recursive algorithm for the last time is continued from the execution progress of the recursive algorithm for the last time based on the obtained result until the obtained result is the result of executing the recursive algorithm for the target variable. The recursive algorithm can be realized on the premise of not using a stack, so that the method is not limited by the depth of the stack, and therefore, the recursive algorithm with higher complexity can be realized.

Description

一种递归算法实现方法、装置及电子设备A recursive algorithm implementation method, device and electronic device

技术领域Technical Field

本发明涉及计算机技术领域,特别是涉及一种递归算法实现方法、装置及电子设备。The present invention relates to the field of computer technology, and in particular to a recursive algorithm implementation method, device and electronic equipment.

背景技术Background Art

递归算法为在算法执行流程中调用自身的算法,往往被设计用于解决复杂度较高的问题。相关技术中,递归算法通常是通过递归函数的方式实现的,示例性的,以计算斐波那契数列第x位的数值为例,则可以通过如下所示的递归函数实现:A recursive algorithm is an algorithm that calls itself during the algorithm execution process, and is often designed to solve complex problems. In related technologies, a recursive algorithm is usually implemented by a recursive function. For example, taking the calculation of the value of the xth digit of the Fibonacci sequence as an example, it can be implemented by a recursive function as shown below:

可见,递归函数中调用该递归函数自身,因此在执行递归函数时,将一次或多次调用该递归函数自身,示例性的,假设需要执行fun(3),则需要调用fun(2)、fun(1),并在fun(2)、fun(1)执行完成后返回执行fun(3),以根据fun(2)、fun(1)的返回值确定fun(3)的返回值。在该过程中,递归函数的输入参数会发生变化,例如在执行fun(3)时,输入参数x=3,在执行fun(2)时输入参数x=2,在返回执行fun(3)时,输入参数x=3。It can be seen that the recursive function calls itself in the recursive function, so when executing the recursive function, the recursive function itself will be called once or multiple times. For example, if fun(3) needs to be executed, fun(2) and fun(1) need to be called, and after fun(2) and fun(1) are executed, fun(3) is returned to be executed, so as to determine the return value of fun(3) according to the return values of fun(2) and fun(1). In this process, the input parameters of the recursive function will change. For example, when executing fun(3), the input parameter x=3, when executing fun(2), the input parameter x=2, and when returning to execute fun(3), the input parameter x=3.

相关技术中,在递归函数的执行过程中,可以利用堆栈存储这些输入参数,堆栈会将新保存的数据存放于堆栈顶部,且进程通过访问堆栈的顶部获得最新的输入参数。示例性的,假设需要执行fun(3),则在堆栈中保存3于堆栈的顶部,执行递归函数的进程可以通过访问堆栈的顶部获取x=3。当在执行fun(3)的过程中调用fun(2)时,在堆栈的顶部保存2,由于堆栈会将新保存的数据存放于堆栈顶部,因此此时执行递归函数的进程访问堆栈的顶部以获取x=2。在fun(2)执行完成后,需要返回执行fun(3)时,可以在堆栈中删除2,此时3重新位于堆栈顶部,因此执行递归函数的进程访问堆栈的顶部可以获取x=3。In the related art, during the execution of a recursive function, a stack can be used to store these input parameters. The stack will store the newly saved data at the top of the stack, and the process obtains the latest input parameters by accessing the top of the stack. For example, assuming that fun(3) needs to be executed, 3 is saved at the top of the stack in the stack, and the process executing the recursive function can obtain x=3 by accessing the top of the stack. When fun(2) is called during the execution of fun(3), 2 is saved at the top of the stack. Since the stack will store the newly saved data at the top of the stack, the process executing the recursive function accesses the top of the stack to obtain x=2. After fun(2) is executed, when it is necessary to return to execute fun(3), 2 can be deleted from the stack. At this time, 3 is back at the top of the stack, so the process executing the recursive function can obtain x=3 by accessing the top of the stack.

但是,对于被设计用于解决复杂度过高的问题的递归算法,如被设计用于对大量数据进行处理的递归算法,在通过递归函数实现该递归算法时,递归函数中可能调用该递归函数自身的次数可能过多,而堆栈的深度有限,无法存储每次调用时的变量(例如上述输入参数),导致无法正常执行该递归函数。因此,难以通过递归函数实现该递归算法,导致这些复杂度较高的问题无法被解决。However, for a recursive algorithm designed to solve a problem with excessive complexity, such as a recursive algorithm designed to process a large amount of data, when the recursive algorithm is implemented through a recursive function, the recursive function itself may be called too many times in the recursive function, and the stack depth is limited, and the variables (such as the above-mentioned input parameters) at each call cannot be stored, resulting in the inability to execute the recursive function normally. Therefore, it is difficult to implement the recursive algorithm through a recursive function, resulting in these highly complex problems being unable to be solved.

发明内容Summary of the invention

本发明实施例的目的在于提供一种递归算法实现方法、装置及电子设备,以实现通过递归算法解决复杂度较高的问题。具体技术方案如下:The purpose of the embodiments of the present invention is to provide a method, device and electronic device for implementing a recursive algorithm, so as to solve a problem with high complexity through a recursive algorithm. The specific technical solution is as follows:

在本发明实的第一方面,提供了一种递归算法实现方法,所述方法包括:In a first aspect of the present invention, a recursive algorithm implementation method is provided, the method comprising:

针对当前变量,开始执行递归算法,所述当前变量的初始值为目标变量;For the current variable, a recursive algorithm is started, wherein the initial value of the current variable is the target variable;

在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,所述递归步骤为需要调用所述递归算法的步骤;During the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, and the recursive step is the step that needs to call the recursive algorithm;

在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,所述上一次执行递归算法为针对上一个当前变量执行的递归算法。During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm continues from the execution progress of the last execution of the recursive algorithm until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.

在本发明的第二方面,提供了一种递归算法实现装置,所述装置包括:In a second aspect of the present invention, a recursive algorithm implementation device is provided, the device comprising:

递归执行模块,用于针对当前变量,开始执行递归算法,所述当前变量的初始值为目标变量;A recursive execution module, used to start executing a recursive algorithm for a current variable, wherein the initial value of the current variable is a target variable;

执行进度记录模块,用于在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,所述递归步骤为需要调用所述递归算法的步骤;An execution progress recording module is used to record the execution progress of the recursive algorithm in the process of executing the recursive algorithm, whenever the executed step is a recursive step, and the recursive step is a step that needs to call the recursive algorithm;

返回执行模块,用于在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,所述上一次执行递归算法为针对上一个当前变量执行的递归算法。The return execution module is used to, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, continue to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm based on the obtained result, until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.

在本发明的第三方面,提供了一种电子设备,包括:In a third aspect of the present invention, there is provided an electronic device, comprising:

存储器,用于存放计算机程序;Memory, used to store computer programs;

处理器,用于执行存储器上所存放的程序时,实现上述第一方面任一所述的方法步骤。The processor is used to implement any method step described in the first aspect when executing the program stored in the memory.

在本发明的第四方面,提供了一种计算机可读存储介质,所述计算机可读存储介质内存储有计算机程序,所述计算机程序被处理器执行时实现上述第一方面任一所述的方法步骤。In a fourth aspect of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, any method step described in the first aspect is implemented.

本发明实施例提供的递归算法实现方法、装置及电子设备,可以通过维护执行进度的方式,使得在完成一次递归算法的调用后能够继续执行之前的递归算法,从而将递归执行过程转化为循环执行的过程,从而在不使用堆栈前提下实现了递归算法,因此不会受到堆栈深度的限制,因此可以实现复杂度较高的递归算法。当然,实施本发明的任一产品或方法并不一定需要同时达到以上所述的所有优点。The recursive algorithm implementation method, device and electronic device provided by the embodiment of the present invention can maintain the execution progress so that the previous recursive algorithm can be continued to be executed after the call of a recursive algorithm is completed, thereby converting the recursive execution process into a loop execution process, thereby implementing the recursive algorithm without using a stack, and thus will not be limited by the stack depth, so that a recursive algorithm with higher complexity can be implemented. Of course, any product or method implementing the present invention does not necessarily need to achieve all the advantages described above at the same time.

附图说明BRIEF DESCRIPTION OF THE DRAWINGS

为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

图1a为本发明实施例提供的堆栈存储数据的一种示意图;FIG. 1a is a schematic diagram of a stack storing data provided by an embodiment of the present invention;

图1b为本发明实施例提供的堆栈存储数据的另一种示意图;FIG1b is another schematic diagram of stack storage data provided by an embodiment of the present invention;

图1c为本发明实施例提供的堆栈存储数据的另一种示意图;FIG1c is another schematic diagram of stack storage data provided by an embodiment of the present invention;

图2为本发明实施例提供的递归算法实现方法的一种流程示意图;FIG2 is a schematic diagram of a flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention;

图3为本发明实施例提供的递归算法实现方法的另一种流程示意图;FIG3 is another schematic flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention;

图4为本发明实施例提供的递归算法实现方法的另一种流程示意图;FIG4 is another schematic flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention;

图5为本发明实施例提供的递归算法实现方法的另一种流程示意图;FIG5 is another schematic flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention;

图6为本发明实施例提供的递归算法实现装置的一种结构示意图;FIG6 is a schematic diagram of a structure of a recursive algorithm implementation device provided by an embodiment of the present invention;

图7为本发明实施例提供的电子设备的一种结构示意图。FIG. 7 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention.

具体实施方式DETAILED DESCRIPTION

下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

为更清楚的对本发明实施例提供的递归函数执行方法进行说明,下面将对递归算法进行说明。对于复杂度较高的问题,开发人员可能难以设计出直接解决该问题的算法,开发人员可以对该问题进行逐级简化,得到多个相似的子问题,通过解决这些子问题以解决该问题。In order to more clearly explain the recursive function execution method provided by the embodiment of the present invention, the recursive algorithm will be explained below. For a problem with high complexity, it may be difficult for a developer to design an algorithm to directly solve the problem. The developer can simplify the problem step by step to obtain multiple similar sub-problems, and solve the problem by solving these sub-problems.

例如假设需要计算n!(n的阶乘),由于n!=n*(n-1)!,因此可以将该问题等价于计算n-1的阶乘,再将n-1的阶乘与n相乘,即该问题可以简化为计算n-1的阶乘,同理计算n-1的阶乘可以被简化为计算n-2的阶乘。基于该思想,可以设计包含以下步骤的递归算法用于计算n!:For example, suppose we need to calculate n! (n factorial). Since n! = n*(n-1)!, we can equate this problem to calculating n-1 factorial, and then multiplying n-1 factorial by n. That is, this problem can be simplified to calculating n-1 factorial. Similarly, calculating n-1 factorial can be simplified to calculating n-2 factorial. Based on this idea, we can design a recursive algorithm that includes the following steps to calculate n!:

第一个步骤:计算(n-1)!;First step: calculate (n-1)! ;

第二个步骤:计算n*(n-1)!。The second step: calculate n*(n-1)! .

其中,第一个步骤中的(n-1)!,如果n大于1,则可以同样通过该递归算法计算得到,如果n等于1,则由于0!=1,因此可以直接得到(n-1)!=1。出于实际需求,也可以设计其他形式的递归算法用于计算n!,可以理解的是,如何设计递归算法并非本发明实施例的改进点,因此在此不再赘述。Among them, (n-1)! in the first step, if n is greater than 1, can also be calculated by the recursive algorithm. If n is equal to 1, since 0! = 1, (n-1)! = 1 can be directly obtained. For practical needs, other forms of recursive algorithms can also be designed to calculate n!. It can be understood that how to design a recursive algorithm is not an improvement point of the embodiment of the present invention, so it will not be repeated here.

相关技术中,可以采用递归函数的方式实现上述递归算法,递归函数可以如下所示:In the related art, the above recursive algorithm can be implemented in the form of a recursive function, and the recursive function can be as follows:

由于在实现递归算法的过程中,递归函数往往需要被多次调用,并且每次调用递归函数时的变量值不同,因此进程在执行递归函数时需要依赖于堆栈缓存每次调用递归函数时的变量值,以执行fun(2)为例,可以在堆栈的顶部存入变量值2,此时堆栈中的数据如图1a所示,进程访问堆栈的顶部,以变量值2第一次调用该递归函数,执行fun(2);由于2不等于0,需要再次以变量值1调用该递归函数,可以中断本次调用,并将变量值1存入堆栈顶部,此时堆栈中的数据如图1b所示,进程访问堆栈的顶部,以变量值1第二次调用该递归函数,执行fun(1)。同理,由于1不等于0,需要再次以变量值0调用该递归函数,可以中断本次调用,并将变量值0存入堆栈顶部,此时堆栈中的数据如图1c所示,进程访问堆栈的顶部,以变量值0第三次调用递归函数,,执行fun(0);由于此时变量值为0,因此第三次调用可以正常结束,并返回fun(0)的计算结果1,并删除堆栈顶部的数据,此时堆栈中的数据如图1b所示,变量值1位于堆栈顶部,进程访问堆栈的顶部,使得之前中断的第二次调用恢复,通过访问堆栈的顶部可以获取x=1,执行fun(1),由于fun(1)=1*fun(0),根据第三次调用该递归函数执行fun(0)的返回值为1,可以计算得到第二次调用该递归函数执行fun(1)的返回值为1*1=1。因此第二次调用正常结束,并返回1,删除堆栈顶部的数据,此时堆栈中的数据如图1a所示,变量值2位于堆栈顶部,进程访问堆栈的顶部,使得之前中断的第一次调用恢复,通过访问堆栈的顶部可以获取x=2,执行fun(2),由于fun(2)=2*fun(1),根据第二次调用该递归函数执行fun(1)的返回值为1,可以计算得到第一次调用该递归函数执行fun(2)的返回值为2*1=2,fun(2)执行完成,返回结果2。Since in the process of implementing a recursive algorithm, a recursive function often needs to be called multiple times, and the variable value is different each time the recursive function is called, the process needs to rely on the stack to cache the variable value each time the recursive function is called when executing a recursive function. Taking the execution of fun(2) as an example, the variable value 2 can be stored at the top of the stack. At this time, the data in the stack is shown in Figure 1a. The process accesses the top of the stack and calls the recursive function for the first time with the variable value 2 to execute fun(2). Since 2 is not equal to 0, the recursive function needs to be called again with the variable value 1. The current call can be interrupted and the variable value 1 can be stored at the top of the stack. At this time, the data in the stack is shown in Figure 1b. The process accesses the top of the stack and calls the recursive function for the second time with the variable value 1 to execute fun(1). Similarly, since 1 is not equal to 0, the recursive function needs to be called again with the variable value 0. The current call can be interrupted and the variable value 0 can be stored at the top of the stack. The data in the stack at this time is shown in Figure 1c. The process accesses the top of the stack and calls the recursive function for the third time with the variable value 0 to execute fun(0). Since the variable value is 0 at this time, the third call can end normally and return the calculation result 1 of fun(0), and delete the data at the top of the stack. The data in the stack at this time is shown in Figure 1b. The variable value 1 is at the top of the stack. The process accesses the top of the stack, so that the second call that was interrupted before is restored. By accessing the top of the stack, x=1 can be obtained and fun(1) is executed. Since fun(1)=1*fun(0), according to the return value of the third call to the recursive function to execute fun(0) is 1, it can be calculated that the return value of the second call to the recursive function to execute fun(1) is 1*1=1. Therefore, the second call ends normally and returns 1, deleting the data at the top of the stack. At this time, the data in the stack is as shown in Figure 1a. The variable value 2 is at the top of the stack. The process accesses the top of the stack, so that the first call that was interrupted before is restored. By accessing the top of the stack, x=2 can be obtained, and fun(2) is executed. Since fun(2)=2*fun(1), according to the return value of the second call to the recursive function to execute fun(1) is 1, it can be calculated that the return value of the first call to the recursive function to execute fun(2) is 2*1=2. Fun(2) is executed and the result 2 is returned.

但是,如果在执行过程中,需要调用该递归函数的次数过多,例如该次数多于堆栈的深度,则堆栈无法保存每次调用递归函数的变量值,将会发生堆栈溢出,造成卡顿或崩溃,导致函数无法被正常执行,即递归算法无法实现。因此,难以通过递归函数实现复杂度过高的递归算法,例如利用递归函数实现二分查找法(一种递归算法)时,如果待查表中所包括的元素过多时,难以通过递归函数实现二分查找法。However, if the recursive function needs to be called too many times during execution, for example, the number of times is greater than the depth of the stack, the stack cannot save the variable values of each call to the recursive function, and a stack overflow will occur, causing a freeze or crash, resulting in the function being unable to be executed normally, that is, the recursive algorithm cannot be implemented. Therefore, it is difficult to implement a recursive algorithm with high complexity through a recursive function. For example, when a binary search method (a recursive algorithm) is implemented using a recursive function, if there are too many elements included in the table to be looked up, it is difficult to implement the binary search method through a recursive function.

基于此,本发明实施例提供了一种递归函数执行方法,可以参见图2,图2所示为本发明实施例提供的递归函数执行方法的一种流程示意图,可以包括:Based on this, an embodiment of the present invention provides a recursive function execution method, as shown in FIG2 , which is a flow chart of a recursive function execution method provided by an embodiment of the present invention, which may include:

S201,针对当前变量,开始执行递归算法,当前变量的初始值为目标变量。S201, start executing the recursive algorithm for the current variable, and the initial value of the current variable is the target variable.

S202,在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,递归步骤为需要调用递归算法的步骤。S202, during the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded. The recursive step is a step that needs to call the recursive algorithm.

S203,在执行递归算法过程中,每当得到针对当前变量执行递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至得到的结果为针对目标变量执行递归算法的结果,上一次执行递归算法为针对上一个当前变量执行的递归算法。S203, during the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm continues from the execution progress of the last execution of the recursive algorithm, until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the previous current variable.

选用该实施例,可以通过维护执行进度的方式,使得在完成一次递归算法的调用后能够继续执行之前的递归算法,从而将递归执行过程转化为循环执行的过程,从而在不使用堆栈前提下实现了递归算法,因此不会受到堆栈深度的限制,因此可以实现复杂度较高的递归算法。By selecting this embodiment, the execution progress can be maintained so that the previous recursive algorithm can be continued to be executed after completing the call of a recursive algorithm, thereby converting the recursive execution process into a loop execution process, thereby implementing the recursive algorithm without using a stack, and therefore will not be limited by the stack depth, so a recursive algorithm with higher complexity can be implemented.

在S201中,目标变量可以是指用户设计递归算法时需要针对的变量值,目标变量根据应用场景的不同可以不同,并且目标变量可以是数值,也可以是非数值,例如,假设用户需要计算6000!,则目标变量为6000。又例如,假设用户需要在预设数列中查找指定元素,则目标变量为预设数列。In S201, the target variable may refer to the variable value that the user needs to target when designing the recursive algorithm. The target variable may be different according to different application scenarios, and the target variable may be a numerical value or a non-numerical value. For example, assuming that the user needs to calculate 6000!, the target variable is 6000. For another example, assuming that the user needs to find a specified element in a preset sequence, the target variable is the preset sequence.

递归算法的执行可以是按照递归算法所设计的步骤依次执行递归算法中的各个步骤。可以理解的是,递归算法中存在一些需要调用递归算法的步骤,示例性的,以前述用于计算n!的递归算法为例,其中的第一个步骤当n大于0时,(n-1)!无法直接计算得到,需要调用该递归算法才能够计算得到(n-1)!,本文称这些需要调用递归算法的步骤为递归步骤,称不需要调用递归算法的步骤为非递归步骤,理论上递归算法中应当包括至少一个递归步骤和至少一个非递归步骤。递归算法中递归步骤和非递归步骤的个数取决于递归算法的设计,而递归算法的设计并非本发明实施例的改进点,因此在此不再赘述。The execution of a recursive algorithm may be to execute each step of the recursive algorithm in sequence according to the steps designed by the recursive algorithm. It is understandable that there are some steps in the recursive algorithm that need to call the recursive algorithm. For example, taking the aforementioned recursive algorithm for calculating n! as an example, in the first step, when n is greater than 0, (n-1)! cannot be directly calculated, and the recursive algorithm needs to be called to calculate (n-1)!. These steps that need to call the recursive algorithm are referred to as recursive steps in this article, and the steps that do not need to call the recursive algorithm are referred to as non-recursive steps. In theory, the recursive algorithm should include at least one recursive step and at least one non-recursive step. The number of recursive steps and non-recursive steps in the recursive algorithm depends on the design of the recursive algorithm, and the design of the recursive algorithm is not an improvement point of the embodiment of the present invention, so it will not be repeated here.

在本实施例中,对于非递归步骤,可以直接计算得到该非递归步骤的结果,而对于递归步骤,则需要针对新的当前变量调用递归算法计算以得到该递归步骤的结果,以前述用于计算n!的递归算法为例,开始执行递归算法时当前变量为n,在第一次执行第一个步骤后,需要针对n-1调用递归算法以得到(n-1)!,此时当前变量变化为n-1。In this embodiment, for a non-recursive step, the result of the non-recursive step can be directly calculated, while for a recursive step, it is necessary to call the recursive algorithm for the new current variable to obtain the result of the recursive step. Taking the aforementioned recursive algorithm for calculating n! as an example, when the recursive algorithm is started, the current variable is n. After the first step is executed for the first time, the recursive algorithm needs to be called for n-1 to obtain (n-1)!, and the current variable changes to n-1.

在S202中,本次执行递归算法是指针对当前变量执行的递归算法,执行进度用于表示本次执行递归算法的执行完成情况,例如,以前述用于计算n!的递归算法为例,其中第一个步骤为递归步骤,假设当前变量为100,则在针对100执行第一个步骤时,由于第一个步骤为递归步骤,因此可以记录用于表示“针对变量值100执行的递归算法接下来要执行第二个步骤”或“针对变量值100执行的递归算法已经执行第一个步骤”的信息作为执行进度。执行进度的表示形式根据应用场景的不同可以不同,示例性的,可以是将第二个步骤的步骤标识作为执行进度以表示“针对变量值100执行的递归算法接下来要执行第二个步骤”,也可以是将第一个步骤的步骤标识作为执行进度以表示“针对变量值100执行的递归算法已经执行第一个步骤”,其中,步骤标识为递归算法中步骤的唯一标识。In S202, the recursive algorithm executed this time refers to the recursive algorithm executed for the current variable, and the execution progress is used to indicate the execution completion status of the recursive algorithm executed this time. For example, taking the aforementioned recursive algorithm for calculating n! as an example, the first step is a recursive step. Assuming that the current variable is 100, when the first step is executed for 100, since the first step is a recursive step, information indicating that "the recursive algorithm executed for the variable value 100 will next execute the second step" or "the recursive algorithm executed for the variable value 100 has already executed the first step" can be recorded as the execution progress. The representation form of the execution progress may be different according to different application scenarios. For example, the step identifier of the second step may be used as the execution progress to indicate that "the recursive algorithm executed for the variable value 100 will next execute the second step", or the step identifier of the first step may be used as the execution progress to indicate that "the recursive algorithm executed for the variable value 100 has already executed the first step", wherein the step identifier is the unique identifier of the step in the recursive algorithm.

在S203中,针对当前变量执行递归算法的结果是通过完成针对当前变量的递归算法得到的。如果当前变量不为目标变量,则得到的结果并非用户需要通过递归算法得到的结果,示例性的,以前述计算n!的递归算法为例,用户需要通过递归算法得到的结果为n!,如果当前变量为n-3,则针对当前变量执行递归算法的结果为(n-3)!,并非用户需要的结果。需要利用(n-3)!继续针对n-2的递归算法,以得到用户需要的结果。In S203, the result of executing the recursive algorithm for the current variable is obtained by completing the recursive algorithm for the current variable. If the current variable is not the target variable, the result obtained is not the result that the user needs to obtain through the recursive algorithm. For example, taking the aforementioned recursive algorithm for calculating n! as an example, the result that the user needs to obtain through the recursive algorithm is n!. If the current variable is n-3, the result of executing the recursive algorithm for the current variable is (n-3)!, which is not the result that the user needs. It is necessary to use (n-3)! to continue the recursive algorithm for n-2 to obtain the result that the user needs.

继续执行上一次执行递归算法时所针对的对象为上一个当前变量,在一些应用场景中上一个当前变量可以是根据当前变量推算得到的,以前述用于计算n!的递归算法为例,假设当前变量为j,则上一个当前变量为j+1。在一些应用场景中上一个当前变量可能无法根据当前变量推算得到,例如用于计算斐波那契数列第n位的数值的递归算法中,如果当前变量为j,则上一个当前变量可能为j+1也可能为j+2。基于此,在一种可能的实施例中,在执行递归算法中,每当所执行的步骤为递归步骤时,记录当前变量,作为本次执行递归算法的输入参量。在继续执行上一次执行递归算法时,可以针对上一次执行递归算法的输入参数,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,该实施例可以适用于上一个当前变量可能无法根据当前变量推算得到的应用场景中,该实施例同样也可以适用于上一个当前变量可以是根据当前变量推算得到的应用场景中。The object targeted when continuing to execute the last recursive algorithm is the last current variable. In some application scenarios, the last current variable can be calculated based on the current variable. Taking the aforementioned recursive algorithm for calculating n! as an example, assuming that the current variable is j, the last current variable is j+1. In some application scenarios, the last current variable may not be calculated based on the current variable. For example, in the recursive algorithm for calculating the value of the nth position of the Fibonacci sequence, if the current variable is j, the last current variable may be j+1 or j+2. Based on this, in a possible embodiment, in the execution of the recursive algorithm, whenever the executed step is a recursive step, the current variable is recorded as the input parameter of the recursive algorithm this time. When continuing to execute the last execution of the recursive algorithm, the last execution of the recursive algorithm can be continued from the execution progress of the last execution of the recursive algorithm for the input parameters of the last execution of the recursive algorithm. This embodiment can be applied to the application scenario where the last current variable may not be calculated based on the current variable, and this embodiment can also be applied to the application scenario where the last current variable can be calculated based on the current variable.

为了更清楚的对本发明实施例提供的递归算法进行说明,下面将结合具体的应用场景对本发明实施例提供的递归算法进行示例性说明,可以理解的是由于在实现递归算法的过程中需要调用递归算法,因此在实现递归算法的过程中需要执行的步骤多于递归算法所设计的步骤。本文中为区别递归算法所设计的步骤与实现递归算法时所执行的步骤,以Step表示实现递归算法时所执行的步骤。In order to more clearly explain the recursive algorithm provided by the embodiment of the present invention, the recursive algorithm provided by the embodiment of the present invention will be exemplarily explained in combination with a specific application scenario. It can be understood that since the recursive algorithm needs to be called in the process of implementing the recursive algorithm, the steps that need to be executed in the process of implementing the recursive algorithm are more than the steps designed by the recursive algorithm. In this article, in order to distinguish the steps designed by the recursive algorithm from the steps executed when implementing the recursive algorithm, the steps executed when implementing the recursive algorithm are represented by Step.

应用场景一:计算3!,并且假设所设计的递归算法为:Application scenario 1: Calculate 3! , and assume that the designed recursive algorithm is:

第一个步骤、a=(x-1)!The first step, a=(x-1)!

第二个步骤、result=x*aThe second step, result = x*a

其中,result表示递归算法的结果,x表示输入。该递归算法的终止条件为x=1,满足该条件时输出的结果1。Among them, result represents the result of the recursive algorithm, and x represents the input. The termination condition of the recursive algorithm is x=1, and the output result 1 is met when the condition is met.

则目标变量为3,因此当前变量初始时为3The target variable is 3, so the current variable is initially 3

Step1:针对3开始执行递归算法。Step 1: Start executing the recursive algorithm for 3.

Step2:针对3执行递归算法的第一个步骤为计算2!,该步骤是递归步骤,因此记录针对3执行递归算法的执行进度。Step 2: The first step of executing the recursive algorithm for 3 is to calculate 2! This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.

该执行进度可以用于表示已经针对3的递归算法已经执行第一个步骤,也可以用于表示针对3的递归算法接下来需要执行第二个步骤,这两种表示方法不同,但是实际上表示的进度是一致的。The execution progress can be used to indicate that the recursive algorithm for 3 has executed the first step, or it can be used to indicate that the recursive algorithm for 3 needs to execute the second step next. The two representation methods are different, but the progress they represent is actually the same.

Step3:开始下一轮迭代,此时当前变量变为2,针对2开始执行递归算法。Step 3: Start the next round of iteration. At this time, the current variable becomes 2, and the recursive algorithm starts to execute for 2.

Step4:针对2执行递归算法的第一个步骤为计算1!,该步骤依然为递归步骤,因此记录针对2执行递归算法的执行进度。Step 4: The first step of executing the recursive algorithm for 2 is to calculate 1! This step is still a recursive step, so the execution progress of the recursive algorithm for 2 is recorded.

Step5:开始下一轮迭代,此时当前变量变为1,针对1开始执行递归算法。此时触发递归算法的终止条件,可以得到针对1执行递归算法的结果为1。Step 5: Start the next iteration. At this time, the current variable becomes 1, and the recursive algorithm starts to execute for 1. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 1 is 1.

Step7:由于得到了结果,因此返回上一次迭代,即返回针对2执行递归算法,由于之前记录的执行进度表示已经针对2执行了递归算法的第一个步骤,因此根据记录的执行进度,接下来针对2执行递归算法的第二个步骤。在执行第二个步骤时,将之前得到的结果“1”作为针对2执行递归算法的第一个步骤得到的a,即a=1,此时可以计算得到result=2*1=2,即得到了针对2执行递归算法的结果为2。Step 7: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 2. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 2, the second step of the recursive algorithm is executed for 2 according to the recorded execution progress. When executing the second step, the previously obtained result "1" is used as a obtained by executing the first step of the recursive algorithm for 2, that is, a = 1. At this time, result = 2*1 = 2 can be calculated, that is, the result of executing the recursive algorithm for 2 is 2.

Step8:由于得到了结果,因此返回上一次迭代,即返回针对1执行递归算法,由于之前记录的执行进度表示已经针对1执行了递归算法的第一个步骤,因此根据记录的执行进度,接下来针对1执行递归算法的第二个步骤。在执行第二个步骤时,将之前得到的结果“2”作为针对1执行递归算法的第一个步骤得到的a,即a=2,此时可以计算得到result=3*2=6,即得到了针对1执行递归算法的结果为6。此时已经得到了针对目标变量执行递归算法的结果,因此流程结束,得到了3!=6。Step 8: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 1. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 1, the second step of the recursive algorithm is executed for 1 according to the recorded execution progress. When executing the second step, the previously obtained result "2" is used as a obtained by executing the first step of the recursive algorithm for 1, that is, a=2. At this time, result=3*2=6 can be calculated, that is, the result of executing the recursive algorithm for 1 is 6. At this time, the result of executing the recursive algorithm for the target variable has been obtained, so the process ends and 3! =6 is obtained.

应用场景2:计算斐波那契数列第4位,为描述方便下文将斐波那契数列第i位记为fib(i),则斐波那契数列是指满足递推公式:fib(x)=fib(x-1)+fib(x-2),并且fib(1)=fib(2)=1的数列。假设所设计的递归算法如下所示:Application scenario 2: Calculate the 4th digit of the Fibonacci sequence. For the convenience of description, the Fibonacci sequence digit i is recorded as fib(i) below. The Fibonacci sequence refers to the sequence that satisfies the recursive formula: fib(x) = fib(x-1) + fib(x-2), and fib(1) = fib(2) = 1. Assume that the designed recursive algorithm is as follows:

第一个步骤:a=fib(x-1)The first step: a = fib(x-1)

第二个步骤:b=fib(x-2)Second step: b = fib(x-2)

第三个步骤:result=a+bThe third step: result = a + b

其中,result表示递归算法的结果,x表示输入。该递归算法的终止条件为x小于等于2,满足该条件时输出的结果1。Among them, result represents the result of the recursive algorithm, and x represents the input. The termination condition of the recursive algorithm is that x is less than or equal to 2, and the output result 1 is met when this condition is met.

则目标变量为4,因此当前变量初始时为4。The target variable is 4, so the current variable is initially 4.

Step1:针对4执行递归算法。Step 1: Execute the recursive algorithm for 4.

Step2:针对4执行递归算法的第一个步骤为计算fib(3),该步骤是递归步骤,因此记录针对4执行递归算法的执行进度。Step 2: The first step of executing the recursive algorithm for 4 is to calculate fib(3). This step is a recursive step, so the execution progress of the recursive algorithm for 4 is recorded.

该执行进度可以用于表示已经针对4的递归算法已经执行第一个步骤,也可以用于表示针对4的递归算法接下来需要执行第二个步骤,这两种表示方法不同,但是实际上表示的进度是一致的。The execution progress can be used to indicate that the recursive algorithm for 4 has executed the first step, or it can be used to indicate that the recursive algorithm for 4 needs to execute the second step next. The two representation methods are different, but the progress they represent is actually the same.

Step3:开始下一轮迭代,此时当前变量变为3,针对3开始执行递归算法。Step 3: Start the next round of iteration. At this time, the current variable becomes 3, and the recursive algorithm starts to execute for 3.

Step4:针对3执行递归算法的第一个步骤为计算fib(2),该步骤是递归步骤,因此记录针对3执行递归算法的执行进度。Step 4: The first step of executing the recursive algorithm for 3 is to calculate fib(2). This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.

Step5:开始下一轮迭代,此时当前变量变为2,针对2开始执行递归算法。此时触发递归算法的终止条件,可以得到针对2执行递归算法的结果为1。Step 5: Start the next iteration. At this time, the current variable becomes 2, and the recursive algorithm starts to execute for 2. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 2 is 1.

Step6:由于得到了结果,因此返回上一次迭代,即返回针对3执行递归算法,由于之前记录的执行进度表示已经针对3执行了递归算法的第一个步骤,因此根据记录的执行进度,接下来针对3执行递归算法的第二个步骤。可以将Step5得到的结果“1”作为针对3执行递归算法的第一个步骤得到的a,即a=1。Step 6: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 3. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 3, the second step of the recursive algorithm is executed for 3 according to the recorded execution progress. The result "1" obtained in Step 5 can be used as a obtained by executing the first step of the recursive algorithm for 3, that is, a = 1.

Step7:针对3执行递归算法的第二个步骤为计算fib(1),该步骤是递归步骤,因此记录针对3执行递归算法的执行进度。Step 7: The second step of the recursive algorithm for 3 is to calculate fib(1). This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.

Step8:开始下一轮迭代,此时当前变量变为1,针对1开始执行递归算法。此时触发递归算法的终止条件,可以得到针对1执行递归算法的结果为1。Step 8: Start the next iteration. At this time, the current variable becomes 1, and the recursive algorithm starts to execute for 1. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 1 is 1.

Step9:由于得到了结果,因此返回上一次迭代,即返回针对3执行递归算法,由于之前记录的执行进度表示已经针对3执行了递归算法的第二个步骤,因此根据记录的执行进度,接下来针对3执行递归算法的第三个步骤。可以将Step8得到的结果“1”作为针对3执行递归算法的第二个步骤得到的b,即b=1。此时可以计算得到result=a+b=2,即得到了针对3执行递归算法的结果为2。Step 9: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 3. Since the previously recorded execution progress indicates that the second step of the recursive algorithm has been executed for 3, the third step of the recursive algorithm is executed for 3 according to the recorded execution progress. The result "1" obtained in Step 8 can be used as b obtained by executing the second step of the recursive algorithm for 3, that is, b = 1. At this time, it can be calculated that result = a + b = 2, that is, the result of executing the recursive algorithm for 3 is 2.

Step10:由于得到了结果,因此返回上一次迭代,即返回针对4执行递归算法,由于之前记录的执行进度表示已经针对4执行了递归算法的第一个步骤,因此根据记录的执行进度,接下来针对4执行递归算法的第二个步骤。可以将Step9得到的结果“2”作为针对4执行递归算法的第一个步骤得到的a,即a=2。Step 10: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 4. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 4, the second step of the recursive algorithm is executed for 4 according to the recorded execution progress. The result "2" obtained in Step 9 can be used as a obtained by executing the first step of the recursive algorithm for 4, that is, a = 2.

Step11:针对4执行递归算法的第二个步骤为计算fib(2),该步骤是递归步骤,因此记录针对4执行递归算法的执行进度。Step 11: The second step of the recursive algorithm for 4 is to calculate fib(2). This step is a recursive step, so the execution progress of the recursive algorithm for 4 is recorded.

Step12:开始下一轮迭代,此时当前变量变为2,针对2开始执行递归算法。此时触发递归算法的终止条件,可以得到针对2执行递归算法的结果为1。Step 12: Start the next iteration. At this time, the current variable becomes 2, and the recursive algorithm starts to execute for 2. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 2 is 1.

Step13:由于得到了结果,因此返回上一次迭代,即返回针对4执行递归算法,由于之前记录的执行进度表示已经针对4执行了递归算法的第二个步骤,因此根据记录的执行进度,接下来针对4执行递归算法的第三个步骤。可以将Step12得到的结果“1”作为针对4执行递归算法的第二个步骤得到的b,即b=1。此时可以计算得到result=a+b=3,即得到了针对4执行递归算法的结果为3。此时已经得到了针对目标变量执行递归算法的结果,因此流程结束,得到了fib(4)=3。Step 13: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 4. Since the previously recorded execution progress indicates that the second step of the recursive algorithm has been executed for 4, the third step of the recursive algorithm is executed for 4 according to the recorded execution progress. The result "1" obtained in Step 12 can be used as b obtained by the second step of the recursive algorithm for 4, that is, b = 1. At this time, it can be calculated that result = a + b = 3, that is, the result of the recursive algorithm for 4 is 3. At this time, the result of the recursive algorithm for the target variable has been obtained, so the process ends and fib(4) = 3 is obtained.

参见图3,图3所示为本发明实施例提供的递归算法实现方法的另一种流程示意图,可以包括:Referring to FIG. 3 , FIG. 3 is another schematic flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention, which may include:

S301,针对当前变量,开始执行递归算法,当前变量的初始值为目标变量。S301, start executing the recursive algorithm for the current variable, and the initial value of the current variable is the target variable.

该步骤与前述S201相同,可以参见前述S201的相关描述,在此不再赘述。This step is the same as the aforementioned S201. Please refer to the relevant description of the aforementioned S201 and will not be repeated here.

S302,每当执行一个步骤时,记录本次执行递归算法的执行进度。S302, each time a step is executed, the execution progress of the recursive algorithm is recorded.

可以理解的是,如果每当执行一个步骤时,均记录本次执行递归算法的执行进度,则每当执行递归步骤时,也将记录本次执行递归算法的执行进度。在本实施例中,如果在记录本次执行递归算法的执行进度之前,已经记录有本次执行递归算法的执行进度,则可以是更新已有的执行进度,也可以是记录一个新的执行进度。示例性的,假设当前变量为7,则在针对7执行递归算法的第一个步骤时,可以记录用于表示“针对变量值7执行的递归算法接下来要执行第二个步骤”的执行进度,在针对7执行递归算法的第二个步骤时,可以将原先记录的用于表示“针对变量值7执行的递归算法接下来要执行第二个步骤”的执行进度,更新为用于表示“针对变量值7执行的递归算法接下来要执行第三个步骤”的执行进度,也可以是保留原先记录的用于表示“针对变量值7执行的递归算法接下来要执行第二个步骤”的执行进度,并记录一个新的用于表示“针对变量值7执行的递归算法接下来要执行第三个步骤”的执行进度。It is understandable that if the execution progress of the recursive algorithm is recorded each time a step is executed, the execution progress of the recursive algorithm will also be recorded each time a recursive step is executed. In this embodiment, if the execution progress of the recursive algorithm has been recorded before the execution progress of the recursive algorithm is recorded, the existing execution progress can be updated or a new execution progress can be recorded. Exemplarily, assuming that the current variable is 7, when the first step of the recursive algorithm is executed for 7, the execution progress indicating that "the recursive algorithm executed for the variable value 7 will next execute the second step" can be recorded; when the second step of the recursive algorithm is executed for 7, the execution progress originally recorded to indicate that "the recursive algorithm executed for the variable value 7 will next execute the second step" can be updated to indicate that "the recursive algorithm executed for the variable value 7 will next execute the third step". Alternatively, the execution progress originally recorded to indicate that "the recursive algorithm executed for the variable value 7 will next execute the second step" can be retained, and a new execution progress indicating that "the recursive algorithm executed for the variable value 7 will next execute the third step" can be recorded.

S303,每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行递归算法的结果。S303, whenever the execution progress of the current recursive algorithm indicates that the current recursive algorithm is completed, the output of the latest executed step is used as the result of executing the recursive algorithm for the current variable.

示例性的,以前述用于计算n!的递归算法为例,如果执行进度表示针对当前变量的递归算法已经执行完第二个步骤,则可以认为本次执行递归算法完成。For example, taking the aforementioned recursive algorithm for calculating n! as an example, if the execution progress indicates that the recursive algorithm for the current variable has completed the second step, it can be considered that the execution of the recursive algorithm is completed.

S304,基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法。S304: Based on the obtained result, continue to execute the last execution of the recursive algorithm starting from the execution progress of the latest record of the last execution of the recursive algorithm.

关于上一次执行递归算法可以参见前述S203中的相关描述在此不再赘述。最新记录的执行进度是指已经记录的所有执行进度中最后被记录的执行进度。示例性的,假设已经针对上一次执行递归算法记录了执行进度1和执行进度2,其中执行进度1记录于执行进度2之前,则最新记录的执行进度为执行进度2。For the last execution of the recursive algorithm, please refer to the relevant description in the aforementioned S203 and no further details will be given here. The latest recorded execution progress refers to the last recorded execution progress among all the recorded execution progress. For example, assuming that execution progress 1 and execution progress 2 have been recorded for the last execution of the recursive algorithm, where execution progress 1 is recorded before execution progress 2, the latest recorded execution progress is execution progress 2.

选用该实施例,可以统一执行非递归步骤和递归步骤时记录执行进度的逻辑,在记录执行进度时无需区别非递归步骤和递归步骤,可以简化递归算法的实现逻辑。By selecting this embodiment, the logic for recording the execution progress when executing non-recursive steps and recursive steps can be unified. There is no need to distinguish between non-recursive steps and recursive steps when recording the execution progress, which can simplify the implementation logic of the recursive algorithm.

下面将对执行进度的记录方式进行说明,在一种可能的实施例中,可以是通过记录所执行的步骤的步骤标识的形式记录执行进度。步骤标识用于唯一标识递归算法中的步骤,递归算法中的不同步骤的步骤标识不同,步骤标识的形式根据应用场景的不同可以不同,例如可以是数值,也可以是字母,还可以是除数值和字母以外的其他字符,可以是单个字符,也可以是多个字符的组合。The following will describe the method of recording the execution progress. In a possible embodiment, the execution progress may be recorded in the form of a step identifier of the executed step. The step identifier is used to uniquely identify the step in the recursive algorithm. Different steps in the recursive algorithm have different step identifiers. The form of the step identifier may be different depending on the application scenario. For example, it may be a numerical value, a letter, or other characters other than numerical values and letters. It may be a single character or a combination of multiple characters.

在该实施例中,递归算法实现方法可以如图4所示,包括:In this embodiment, the recursive algorithm implementation method may be as shown in FIG4 , including:

S401,针对当前变量,开始执行递归算法,当前变量的初始值为目标变量。S401, start executing the recursive algorithm for the current variable, and the initial value of the current variable is the target variable.

该步骤与前述S201相同,可以参见前述S201的相关描述,在此不再赘述。This step is the same as the aforementioned S201. Please refer to the relevant description of the aforementioned S201 and will not be repeated here.

S402,每当执行一个步骤时,记录所执行的步骤步骤标识。S402, whenever a step is executed, the step identifier of the executed step is recorded.

所记录的步骤标识即为本次执行递归算法的执行进度,可以表示本次递归递归算法已经执行所记录的步骤标识所表示的步骤。在其他可能的实施例中,也可以是记录所执行的步骤的下一个步骤的步骤标识,以表示本次递归递归算法接来下要执行所记录的步骤标识所表示的步骤。两种记录执行进度的方式虽然记录的步骤标识不同,但是所记录的执行进度的表意是等效的。The recorded step identifier is the execution progress of the recursive algorithm this time, which can indicate that the recursive algorithm has executed the step indicated by the recorded step identifier. In other possible embodiments, it can also be the step identifier of the next step of the executed step to indicate that the recursive algorithm will execute the step indicated by the recorded step identifier next. Although the two methods of recording the execution progress record different step identifiers, the meaning of the recorded execution progress is equivalent.

S403,每当本次执行递归算法所记录的步骤标识表示递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行递归算法的结果。S403: Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable.

在一种可能的实施例中,步骤标识可以用于表示距离递归算法中最后一个步骤所剩余的步骤数,递归算法中按照所设计的执行先后顺序最后执行的步骤,递归算法中的最后一个步骤的结果即为递归算法的结果,并且由于最后一个步骤的结果为递归算法的结果,因此最后一个步骤应当为非递归步骤。示例性的,以前述用于计算n!的递归算法为例,最后一个步骤为第二个步骤,第一个步骤距离第二个步骤所剩余的步骤数为1,因此第一个步骤的步骤标识可以1,第二个步骤距离第二个步骤所剩余的步骤数为0,因此第二个步骤的步骤标识可以为0。该实施例中,可以是每当步骤标识表示距离递归算法中最后一个步骤所剩余的步骤数为0时,将最新执行的步骤的输出作为针对当前变量执行递归算法的结果。In a possible embodiment, the step identifier can be used to indicate the number of steps remaining from the last step in the recursive algorithm. The step that is executed last in the recursive algorithm according to the designed execution sequence, the result of the last step in the recursive algorithm is the result of the recursive algorithm, and since the result of the last step is the result of the recursive algorithm, the last step should be a non-recursive step. Exemplarily, taking the aforementioned recursive algorithm for calculating n! as an example, the last step is the second step, the number of steps remaining from the first step to the second step is 1, so the step identifier of the first step can be 1, and the number of steps remaining from the second step to the second step is 0, so the step identifier of the second step can be 0. In this embodiment, whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable.

S404,基于所得到的结果,从上一次执行递归算法的最新记录的步骤标识所表示的步骤开始,继续执行上一次执行递归算法。S404: Based on the obtained result, continue to execute the last execution of the recursive algorithm starting from the step indicated by the step identifier of the latest record of the last execution of the recursive algorithm.

下面将对如何判断所得到的结果是否为对目标变量执行递归算法的结果进行说明:The following will explain how to determine whether the result obtained is the result of executing the recursive algorithm on the target variable:

在一种可能的实施例中,可以是确定本次执行递归算法是否为继续第一次执行递归算法,如果本次执行递归算法不为继续第一次执行递归算法,由于只有第一次执行递归算法所针对的变量为目标变量,因此可以认为本次执行递归算法得到的结果不为对目标变量执行递归算法得到的结果,则可以额从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法。如果本次执行递归算法为继续第一次执行递归算法,由于第一次执行递归算法所针对的变量为目标变量,因此可以认为本次执行递归算法得到的结果为对目标变量执行递归算法得到的结果,则可以将得到的结果作为针对目标变量执行递归算法的结果。In a possible embodiment, it can be determined whether the current execution of the recursive algorithm is a continuation of the first execution of the recursive algorithm. If the current execution of the recursive algorithm is not a continuation of the first execution of the recursive algorithm, since only the variable targeted by the first execution of the recursive algorithm is the target variable, it can be considered that the result obtained by the current execution of the recursive algorithm is not the result obtained by executing the recursive algorithm on the target variable, and the execution of the last execution of the recursive algorithm can be continued from the execution progress of the last execution of the recursive algorithm. If the current execution of the recursive algorithm is a continuation of the first execution of the recursive algorithm, since the variable targeted by the first execution of the recursive algorithm is the target variable, it can be considered that the result obtained by the current execution of the recursive algorithm is the result obtained by executing the recursive algorithm on the target variable, and the obtained result can be used as the result of executing the recursive algorithm on the target variable.

关于如何确定本次执行递归算法是否为继续第一次执行递归算法,可以是判断本次执行递归算法所针对的变量是否为目标变量,如果本次执行递归算法所针对的变量为目标变量,则本次执行递归算法为继续第一次执行递归算法,如果本次执行递归算法所针对的变量不为目标变量,则本次执行递归算法不为继续第一次执行递归算法。Regarding how to determine whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, it can be determined whether the variable targeted by the current execution of the recursive algorithm is the target variable. If the variable targeted by the current execution of the recursive algorithm is the target variable, then the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm. If the variable targeted by the current execution of the recursive algorithm is not the target variable, then the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm.

在一种可能的实施例中,在执行递归算法过程中,每当得到针对当前变量执行递归算法的结果时,删除本次执行递归算法记录的执行进度。则可以是确定是否还记录有执行进度,如果还记录有执行进度,则可以确定本次执行递归算法不为继续第一次执行递归算法,如果没有记录执行进度,确定本次执行递归算法为继续第一次执行递归算法。In a possible embodiment, during the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted. Then, it can be determined whether the execution progress is still recorded. If the execution progress is still recorded, it can be determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm. If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.

在一种可能的实施例中,可以是在一个数组中记录执行进度。示例性的开始维护一个预设数组,预设数组初始时各元素为空(在其他可能的实施例中,初始时各元素也可以是预设初始值),每当记录执行进度时,可以是将执行进度记录于该数组的末尾,其中,数组的末尾是指按照有前到后的顺序数组中的第一个空元素,例如第一次记录执行进度时,由于数组中各元素均为空,因此数组的末尾即为数组的第一位,因此将执行进度记录于数组的第一位。第二次记录执行进度时,由于数组的第一位已经记录有执行进度,因此数组的第一位为非空元素,第二位为空元素,因此数组的末尾为第二位,因此将执行进度记录于数组的第二位。In a possible embodiment, the execution progress may be recorded in an array. Exemplarily, a preset array is maintained, and each element of the preset array is initially empty (in other possible embodiments, each element may also be a preset initial value at the beginning). Whenever the execution progress is recorded, the execution progress may be recorded at the end of the array, wherein the end of the array refers to the first empty element in the array in order from front to back. For example, when the execution progress is recorded for the first time, since each element in the array is empty, the end of the array is the first position of the array, so the execution progress is recorded in the first position of the array. When the execution progress is recorded for the second time, since the first position of the array has already recorded the execution progress, the first position of the array is a non-empty element, and the second position is an empty element, so the end of the array is the second position, so the execution progress is recorded in the second position of the array.

前述的输入参量可以与执行进度对应记录,示例性的,在一种可能的实施例中,可以是将输入参量和执行进度作为结构体中的成员变量,并将该结构体写入数组中,以在数组中对应记录输入参量和执行进度。示例性的,可以是定义一个结构体Scope(作用域),Scope可以包括成员变量n和成员变量mark,将输入参量赋值给Scope中的n,并将执行进度赋值给Scope中的mark,再将该Scope写入数组,则可以实现在数组中对应记录输入参量以及执行进度。The aforementioned input parameters can be recorded corresponding to the execution progress. For example, in a possible embodiment, the input parameters and the execution progress can be used as member variables in a structure, and the structure can be written into an array to record the input parameters and the execution progress in the array. For example, a structure Scope can be defined, and the Scope can include a member variable n and a member variable mark. The input parameter is assigned to n in the Scope, and the execution progress is assigned to mark in the Scope. Then the Scope is written into the array, so that the input parameter and the execution progress can be recorded in the array.

选用该实施例,可以通过数组模拟堆栈入栈、弹栈的工作原理,便于开发人员更好的设计和理解递归算法的实现方式。By selecting this embodiment, the working principle of stack push and pop can be simulated through an array, which is convenient for developers to better design and understand the implementation method of the recursive algorithm.

为了更清楚的对本发明实施例提供的递归算法实现方法进行说明,下面将结合本发明实施例提供的递归算法实现方法的计算机语言,对本发明实施例提供的递归算法实现方法进行详细说明,以前述应用场景2为例,计算机语言可以如下所示:In order to more clearly illustrate the recursive algorithm implementation method provided by the embodiment of the present invention, the recursive algorithm implementation method provided by the embodiment of the present invention will be described in detail below in combination with the computer language of the recursive algorithm implementation method provided by the embodiment of the present invention. Taking the aforementioned application scenario 2 as an example, the computer language can be as follows:

下面将以上述计算机语言为例进行说明,可以参见图5,图5所示为本发明实施例提供的递归算法实现方法的另一种流程示意图,可以包括:The above computer language will be used as an example for explanation below. Please refer to FIG5 , which is another flowchart of the recursive algorithm implementation method provided by the embodiment of the present invention, which may include:

S501,确定待执行的递归算法中调用递归算法的次数n。S501, determining the number n of times a recursive algorithm is called in a recursive algorithm to be executed.

以前述应用场景一为例,递归算法中调用递归算法的次数为1,因此n=1,以前述应用场景二为例,递归算法中调用递归算法的次数为2,因此n=2。Taking the aforementioned application scenario 1 as an example, the number of times the recursive algorithm is called in the recursive algorithm is 1, so n=1. Taking the aforementioned application scenario 2 as an example, the number of times the recursive algorithm is called in the recursive algorithm is 2, so n=2.

S502,根据递归算法,生成n个递归子函数和1个非递归子函数。S502: Generate n recursive sub-functions and 1 non-recursive sub-function according to the recursive algorithm.

其中,每个递归子函数用于实现递归算法中的一个递归步骤,非递归子函数用于实现递归算法中的非递归步骤。可以理解的是,这里的递归子函数只是一种命名,递归子函数本身并没有调用该递归子函数,即递归子函数不为递归函数。示例性的,可以参见前述计算机语言,其中的Each recursive sub-function is used to implement a recursive step in a recursive algorithm, and a non-recursive sub-function is used to implement a non-recursive step in a recursive algorithm. It can be understood that the recursive sub-function here is just a name, and the recursive sub-function itself does not call the recursive sub-function, that is, the recursive sub-function is not a recursive function. For example, see the aforementioned computer language, in which

即为一个递归子函数。其中,Current为当前变量,makeScope为用于生成结构体Scope的自定义函数。stack push为用于向stack的末尾记录Scope的自定义函数,stack为前述预设数组。It is a recursive sub-function. Among them, Current is the current variable, makeScope is a custom function used to generate the structure Scope. Stack push is a custom function used to record Scope at the end of the stack, and stack is the aforementioned preset array.

为描述方便,下文假设生成的子函数分别为{P(1)、P(2)、…、P(n+1)}。可以理解的是,图5所示的实施例仅为本发明实施例提供的递归算法实现方法的一种可能的流程示意图,在其他可能的实施例中,也可以是生成多个非递归子函数,这多个非递归子函数用于共同实现递归算法中的非递归步骤。而通过一个非递归子函数实现递归算法中的非递归步骤,可以有效降低实现递归算法时的复杂度。For the convenience of description, it is assumed below that the generated sub-functions are {P(1), P(2), ..., P(n+1)}. It can be understood that the embodiment shown in FIG5 is only a possible flow chart of the recursive algorithm implementation method provided by the embodiment of the present invention. In other possible embodiments, multiple non-recursive sub-functions can also be generated, and these multiple non-recursive sub-functions are used to jointly implement the non-recursive steps in the recursive algorithm. Implementing the non-recursive steps in the recursive algorithm through a non-recursive sub-function can effectively reduce the complexity of implementing the recursive algorithm.

S503,对第一次迭代操作的对象创建Scope,并将创建的Scope记录于stack的末尾。S503, creating a Scope for the object of the first iteration operation, and recording the created Scope at the end of the stack.

Scope中至少包括两个成员变量:输入参量和执行进度。关于输入参量和执行进度可以参见前述相关说明,在此不再赘述。下文中为描述方便,假设执行进度是以步骤标识表示的,并且步骤标识为距离递归算法中最后一个步骤所剩余的步骤数,并且将执行进度表示为rest。Scope includes at least two member variables: input parameters and execution progress. For input parameters and execution progress, please refer to the above related descriptions, which will not be repeated here. For the convenience of description below, it is assumed that the execution progress is represented by the step identifier, and the step identifier is the number of steps remaining from the last step in the recursive algorithm, and the execution progress is represented as rest.

在该实施例中,初始时由于没有执行递归算法中的任何步骤,因此距离递归算法中最后一个步骤所剩余的步骤数为n+1,即执行进度的初始值为n+1。In this embodiment, since no step in the recursive algorithm is executed initially, the number of steps remaining from the last step in the recursive algorithm is n+1, that is, the initial value of the execution progress is n+1.

S504,读取stack中末尾的元素。S504, read the last element in the stack.

读取末尾的元素是指按照从前到后的顺序读取stack中最后一个非空元素,在一些可能的实施例中,预设数组中如果不包括空元素,则读取预设数组中末尾的元素等价于读取预设数组中按照从前到后的顺序的最后一个元素。Reading the last element means reading the last non-empty element in the stack in order from front to back. In some possible embodiments, if the preset array does not include empty elements, reading the last element in the preset array is equivalent to reading the last element in the preset array in order from front to back.

S505,确定rest是否等于0,如果rest不等于0,执行S506,如果rest等于0,执行S510。S505, determine whether rest is equal to 0, if rest is not equal to 0, execute S506, if rest is equal to 0, execute S510.

该rest为所读取的Scope中的rest。The rest is the rest in the read Scope.

S506,进入P(n+2-rest)。S506, enter P(n+2-rest).

可以理解的是,如果rest为0,则表示已经得到针对当前变量执行递归算法的结果,因此无需继续针对当前变量执行递归算法。如果rest不为0,则表示尚未得到针对当前变量执行递归算法的结果,而如前述关于执行进度的描述,rest则表示接下来需要针对当前变量执行递归算法的中的倒数第rest-1个步骤,由于递归算法中一共包括n+1个步骤,因此倒数第rest-1个步骤即为正数第n+2-rest个步骤,因此进入P(n+2-rest)。It is understandable that if rest is 0, it means that the result of executing the recursive algorithm for the current variable has been obtained, so there is no need to continue to execute the recursive algorithm for the current variable. If rest is not 0, it means that the result of executing the recursive algorithm for the current variable has not been obtained. As described above about the execution progress, rest means that the rest-1th to last step of the recursive algorithm needs to be executed for the current variable. Since the recursive algorithm includes a total of n+1 steps, the rest-1th to last step is the n+2-rest step, so enter P(n+2-rest).

S507,rest自减1。S507, rest is decremented by 1.

将所读取的Scope中的rest自减1。Decrement the rest in the read Scope by 1.

S508,确定所进入的子函数是否为递归子函数,如果为递归子函数,执行S509,如果不为递归子函数,返回S504。S508, determine whether the entered sub-function is a recursive sub-function, if it is a recursive sub-function, execute S509, if it is not a recursive sub-function, return to S504.

S509,对下一次执行递归算法所针对的变量创建Scope,并将创建的Scope记录于stack的末尾。S509, creating a Scope for the variable targeted by the next execution of the recursive algorithm, and recording the created Scope at the end of the stack.

如果所进入的子函数为递归子函数,由于递归子函数用于实现递归算法中的递归步骤,而递归步骤中调用递归算法,下一次执行递归算法所针对的变量即为所进入的递归子函数用于实现的递归步骤中调用递归算法时的变量。If the sub-function entered is a recursive sub-function, since the recursive sub-function is used to implement the recursive step in the recursive algorithm, and the recursive algorithm is called in the recursive step, the variable targeted at the next execution of the recursive algorithm is the variable when the recursive algorithm is called in the recursive step implemented by the recursive sub-function entered.

所创建的Scope中输入参量为下一次执行递归算法所针对的变量,执行进度为n+1。The input parameter in the created Scope is the variable for the next execution of the recursive algorithm, and the execution progress is n+1.

S510,汇总之前各次执行递归算法的结果计算本次执行递归算法的结果。S510, summarizing the results of the previous executions of the recursive algorithm to calculate the result of the current execution of the recursive algorithm.

可以理解的是,如果rest等于0,则表示将要完成本次迭代的计算,而根据递归算法的原理,完成一次执行递归算法的计算,需要使用到之前各次执行递归算法的结果。It is understandable that if rest is equal to 0, it means that the calculation of this iteration is about to be completed. According to the principle of the recursive algorithm, the results of the previous executions of the recursive algorithm need to be used to complete the calculation of the recursive algorithm.

S511,删除预stack中末尾的元素S511, delete the last element in the pre-stack

S512,判断stack是否为空,如果stack不为空,返回执行S504,如果stack为空,执行S513。S512, determine whether the stack is empty. If the stack is not empty, return to execute S504. If the stack is empty, execute S513.

S513,将最后一个得到的结果作为针对目标变量执行递归算法的结果。S513: Use the last result obtained as the result of executing the recursive algorithm for the target variable.

下面将结合前述应用场景2,对该实施例进行示例性说明,为描述方便,以(i,rest)的形式表示Scope,其中,i为Scope中的输入参量,rest为Scope中的执行进度。The embodiment will be exemplarily described below in conjunction with the aforementioned application scenario 2. For the convenience of description, Scope is represented in the form of (i, rest), where i is the input parameter in Scope and rest is the execution progress in Scope.

在计算斐波那契数列的第4位的过程中,由于第一个步骤和第二个步骤均为递归步骤,因此n=2,构建递归子函数P(1)、P(2)以及非递归子函数P(3),其中,P(1)用于实现第一个步骤,P(2)用于实现第二个步骤,P(3)用于实现第三个步骤。In the process of calculating the 4th digit of the Fibonacci sequence, since the first step and the second step are both recursive steps, n=2, and recursive subfunctions P(1), P(2) and non-recursive subfunction P(3) are constructed, where P(1) is used to implement the first step, P(2) is used to implement the second step, and P(3) is used to implement the third step.

在stack中记录(4,3),此时stack为[(4,3)],读取stack中的末尾的元素,即读取(4,3),可以确定rest=3并不等于0,因此进入P(2+2-3)即进入P(1),以针对4执行递归算法的第一个步骤,即计算a=fib(3)。并将读取到的元素中的rest自减1,此时stack为[(4,2)]。Record (4,3) in the stack. At this time, the stack is [(4,3)]. Read the last element in the stack, that is, read (4,3). It can be determined that rest = 3 is not equal to 0. Therefore, enter P(2+2-3) or enter P(1) to perform the first step of the recursive algorithm for 4, that is, calculate a = fib(3). And reduce rest in the read element by 1. At this time, the stack is [(4,2)].

由于针对4执行P(1)为递归子函数,因此对下一次执行递归算法所针对的变量,即3,创建Scope,并将创建的Scope记录于stack的末尾,所创建的Scope为(3,3),在将该Scope记录于stack末尾后,stack为[(4,2),(3,3)]。Since the execution of P(1) for 4 is a recursive sub-function, a Scope is created for the variable targeted by the next execution of the recursive algorithm, that is, 3, and the created Scope is recorded at the end of the stack. The created Scope is (3,3). After the Scope is recorded at the end of the stack, the stack is [(4,2), (3,3)].

再次读取stack末尾的元素,即读取(3,3),可以确定rest=3并不等于0,因此进入P(2+2-3)即进入P(1),以针对3执行递归算法的第一个步骤,即计算a=fib(2)。并将读取到的元素中的rest自减1,此时stack为[(4,2),(3,2)]。Read the element at the end of the stack again, that is, read (3,3), and you can determine that rest = 3 is not equal to 0, so enter P(2+2-3), that is, enter P(1), and execute the first step of the recursive algorithm for 3, that is, calculate a = fib(2). And decrement the rest in the read element by 1, and the stack is now [(4,2), (3,2)].

由于针对3执行P(1)为递归子函数,因此对下一次执行递归算法所针对的变量,即2,创建Scope,并将创建的Scope记录于stack的末尾,所创建的Scope为(2,3),在将该Scope记录于stack末尾后,stack为[(4,2),(3,2),(2,3)]。Since the execution of P(1) for 3 is a recursive sub-function, a Scope is created for the variable targeted by the next execution of the recursive algorithm, that is, 2, and the created Scope is recorded at the end of the stack. The created Scope is (2,3). After the Scope is recorded at the end of the stack, the stack is [(4,2), (3,2), (2,3)].

再次读取stack末尾的元素,即读取(2,3),可以确定rest=3并不等于0,因此进入P(2+2-3)即进入P(1),以针对2执行递归算法的第一个步骤,即计算fib(1)。并将读取到的元素中的rest自减1,此时stack为[(4,2),(3,2),(2,2)]。由于可以直接计算得到fib(1)=1,因此针对2执行P(1)不为递归子函数。Read the element at the end of the stack again, that is, read (2,3), and we can determine that rest = 3 is not equal to 0, so we enter P(2+2-3), that is, enter P(1), and execute the first step of the recursive algorithm for 2, that is, calculate fib(1). And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (3,2), (2,2)]. Since fib(1) = 1 can be directly calculated, executing P(1) for 2 is not a recursive subfunction.

再次读取stack末尾的元素,即读取(2,2),可以确定rest=2并不等于0,因此进入P(2+2-2)即进入P(2),以针对2执行递归算法的第二个步骤,即计算b=fib(0)。并将读取到的元素中的rest自减1,此时stack为[(4,2),(3,2),(2,1)]。由于可以直接计算得到fib(0)=0,因此针对2执行P(2)不为递归子函数。Read the element at the end of the stack again, that is, read (2,2), and we can determine that rest = 2 is not equal to 0, so we enter P(2+2-2), that is, enter P(2), and execute the second step of the recursive algorithm for 2, that is, calculate b = fib(0). And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (3,2), (2,1)]. Since fib(0) = 0 can be directly calculated, executing P(2) for 2 is not a recursive subfunction.

再次读取stack末尾的元素,即读取(2,1),可以确定rest=1并不等于0,因此进入P(2+2-1)即进入P(3),以针对2执行递归算法的第三个步骤,即计算result=a+b。并将读取到的元素中的rest自减1,此时stack为[(4,2),(3,2),(2,0)]。Read the element at the end of the stack again, that is, read (2,1), and you can determine that rest = 1 is not equal to 0, so enter P(2+2-1) or P(3) to perform the third step of the recursive algorithm for 2, that is, calculate result = a + b. And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (3,2), (2,0)].

由于P(3)为非递归子函数,因此再次读取stack末尾的元素,即读取(2,0),可以确定rest=0,此时汇总之前得到的a=1,b=0,可以计算得到本次执行递归算法的结果为fib(2)=a+b=1。删除stack末尾的元素,此时stack为[(4,2),(3,2)]。Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, read (2,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = 1 and b = 0, and calculate that the result of this recursive algorithm is fib(2) = a + b = 1. Delete the element at the end of the stack, and the stack is now [(4,2), (3,2)].

此时stack不为空,因此再次读取stack末尾的元素,即读取(3,2),可以确定rest=2并不等于0,因此进入P(2+2-2)即进入P(2),以针对3执行递归算法的第二个步骤,即计算b=fib(1)。并将读取到的元素中的rest自减1,此时stack为[(4,2),(3,1)。由于可以直接计算得到fib(1)=1,因此针对3执行P(2)不为递归子函数,并且获得a=fib(2)=1。At this time, stack is not empty, so the element at the end of stack is read again, that is, (3,2). It can be determined that rest = 2 is not equal to 0, so enter P(2+2-2), that is, enter P(2) to execute the second step of the recursive algorithm for 3, that is, calculate b = fib(1). And reduce rest in the read element by 1. At this time, stack is [(4,2), (3,1). Since fib(1) = 1 can be directly calculated, executing P(2) for 3 is not a recursive subfunction, and a = fib(2) = 1 is obtained.

再次读取stack末尾的元素,即读取(3,1),可以确定rest=1并不等于0,因此进入P(2+2-1)即进入P(3),以针对3执行递归算法的第三个步骤,即计算result=a+b,并且可以获得b=fib(1)=1。并将读取到的元素中的rest自减1,此时stack为[(4,2),(3,0)]。Read the element at the end of the stack again, that is, read (3,1), and you can determine that rest = 1 is not equal to 0, so enter P(2+2-1), that is, enter P(3), and perform the third step of the recursive algorithm for 3, that is, calculate result = a + b, and you can get b = fib(1) = 1. And decrement the rest in the read element by 1, and the stack is now [(4,2), (3,0)].

由于P(3)为非递归子函数,因此再次读取stack末尾的元素,即读取(3,0),可以确定rest=0,此时汇总之前得到的a=fib(2)=1,b=fib(1)=1,可以计算得到本次执行递归算法的结果为fib(3)=a+b=2。删除stack末尾的元素,此时stack为[(4,2)]。Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, read (3,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = fib(2) = 1 and b = fib(1) = 1, and we can calculate that the result of this recursive algorithm is fib(3) = a + b = 2. Delete the element at the end of the stack, and the stack is now [(4,2)].

此时stack不为空,因此再次读取stack末尾的元素,即读取(4,2),可以确定rest=2并不等于0,因此进入P(2+2-2)即进入P(2),以针对4执行递归算法的第二个步骤,即计算b=fib(2),并且可以获得a=fib(3)=2。并将读取到的元素中的rest自减1,此时stack为[(4,1)]。At this time, stack is not empty, so we read the element at the end of stack again, that is, read (4,2), and we can determine that rest = 2 is not equal to 0, so we enter P(2+2-2), that is, enter P(2), to perform the second step of the recursive algorithm for 4, that is, calculate b = fib(2), and we can get a = fib(3) = 2. And we decrement rest in the read element by 1, and now stack is [(4,1)].

由于针对4执行P(2)为递归子函数,因此对下一次执行递归算法所针对的变量,即2,创建Scope,并将创建的Scope记录于stack的末尾,所创建的Scope为(2,3),在将该Scope记录于stack末尾后,stack为[(4,1),(2,3)]。Since P(2) executed for 4 is a recursive sub-function, a Scope is created for the variable targeted by the next execution of the recursive algorithm, that is, 2, and the created Scope is recorded at the end of the stack. The created Scope is (2,3). After the Scope is recorded at the end of the stack, the stack is [(4,1), (2,3)].

再次读取stack末尾的元素,即读取(2,3),可以确定rest=3并不等于0,因此进入P(2+2-3)即进入P(1),以针对2执行递归算法的第一个步骤,即计算fib(1)。并将读取到的元素中的rest自减1,此时stack为[(4,1),(2,2)]。由于可以直接计算得到fib(1)=1,因此针对2执行P(1)不为递归子函数。Read the element at the end of the stack again, that is, read (2,3), and we can determine that rest = 3 is not equal to 0, so we enter P(2+2-3), that is, enter P(1), to execute the first step of the recursive algorithm for 2, that is, calculate fib(1). And decrement the rest in the read element by 1. At this time, the stack is [(4,1), (2,2)]. Since fib(1) = 1 can be directly calculated, executing P(1) for 2 is not a recursive subfunction.

再次读取stack末尾的元素,即读取(2,2),可以确定rest=2并不等于0,因此进入P(2+2-2)即进入P(2),以针对2执行递归算法的第二个步骤,即计算b=fib(0)。并将读取到的元素中的rest自减1,此时stack为[(4,1),(2,1)]。由于可以直接计算得到fib(0)=0,因此针对2执行P(2)不为递归子函数。Read the element at the end of the stack again, that is, read (2,2), and we can determine that rest = 2 is not equal to 0, so we enter P(2+2-2), that is, enter P(2), to execute the second step of the recursive algorithm for 2, that is, calculate b = fib(0). And decrement the rest in the read element by 1. At this time, the stack is [(4,1), (2,1)]. Since fib(0) = 0 can be directly calculated, executing P(2) for 2 is not a recursive subfunction.

再次读取stack末尾的元素,即读取(2,1),可以确定rest=1并不等于0,因此进入P(2+2-1)即进入P(3),以针对2执行递归算法的第三个步骤,即计算result=a+b。并将读取到的元素中的rest自减1,此时stack为[(4,2),(2,0)]。Read the element at the end of the stack again, that is, read (2,1), and you can determine that rest = 1 is not equal to 0, so enter P(2+2-1) or P(3) to perform the third step of the recursive algorithm for 2, that is, calculate result = a + b. And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (2,0)].

由于P(3)为非递归子函数,因此再次读取stack末尾的元素,即读取(2,0),可以确定rest=0,此时汇总之前得到的a=1,b=0,可以计算得到本次执行递归算法的结果为fib(2)=a+b=1。删除stack末尾的元素,此时stack为[(4,1)]。Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, (2,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = 1 and b = 0, and calculate that the result of this recursive algorithm is fib(2) = a + b = 1. Delete the element at the end of the stack, and the stack is now [(4,1)].

此时stack不为空,因此再次读取stack末尾的元素,即读取(4,1),可以确定rest=1并不等于0,因此进入P(2+2-1)即进入P(3),以针对4执行递归算法的第三个步骤,即计算result=a+b,并且可以获得b=fib(2)=1。并将读取到的元素中的rest自减1,此时stack为[(4,0)]。At this time, stack is not empty, so we read the element at the end of stack again, that is, read (4,1), and we can determine that rest = 1 is not equal to 0, so we enter P(2+2-1), that is, enter P(3), to perform the third step of the recursive algorithm for 4, that is, calculate result = a + b, and we can get b = fib(2) = 1. And we decrement rest in the read element by 1, and now stack is [(4,0)].

由于P(3)为非递归子函数,因此再次读取stack末尾的元素,即读取(4,0),可以确定rest=0,此时汇总之前得到的a=fib(3)=2,b=fib(2)=1,可以计算得到本次执行递归算法的结果为fib(4)=a+b=3。删除stack末尾的元素,此时stack为[],即stack为空,最后一个得到的结果3即为针对4执行递归算法得到的结果。Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, read (4,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = fib(3) = 2 and b = fib(2) = 1, and we can calculate that the result of this recursive algorithm is fib(4) = a + b = 3. Delete the element at the end of the stack. At this time, the stack is [], that is, the stack is empty. The last result 3 is the result of the recursive algorithm for 4.

参见图6,图6所示为本发明实施例提供的递归算法实现装置的一种结构示意图,可以包括:Referring to FIG. 6 , FIG. 6 is a schematic diagram of a structure of a recursive algorithm implementation device provided by an embodiment of the present invention, which may include:

递归执行模块601,用于针对当前变量,开始执行递归算法,所述当前变量的初始值为目标变量;A recursive execution module 601 is used to start executing a recursive algorithm for a current variable, where the initial value of the current variable is a target variable;

执行进度记录模块602,用于在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,所述递归步骤为需要调用所述递归算法的步骤;An execution progress recording module 602 is used to record the execution progress of the recursive algorithm each time the executed step is a recursive step during the execution of the recursive algorithm, wherein the recursive step is a step that needs to call the recursive algorithm;

返回执行模块603,用于在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,所述上一次执行递归算法为针对上一个当前变量执行的递归算法。Return execution module 603 is used to continue executing the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm based on the obtained result whenever the result of executing the recursive algorithm for the current variable is obtained during the execution of the recursive algorithm, until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.

在一种可能的实施例中,所述执行进度记录模块602每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,包括:In a possible embodiment, the execution progress recording module 602 records the execution progress of the recursive algorithm whenever the executed step is a recursive step, including:

每当执行一个步骤时,记录本次执行递归算法的执行进度;Every time a step is executed, the progress of the recursive algorithm is recorded;

所述返回执行模块603每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:Whenever the return execution module 603 obtains the result of executing the recursive algorithm for the current variable, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including:

每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;

基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm.

在一种可能的实施例中,所述执行进度记录模块602记录执行递归算法的执行进度,包括:In a possible embodiment, the execution progress recording module 602 records the execution progress of the recursive algorithm, including:

记录所执行的步骤的步骤标识;The step ID that records the steps performed;

所述返回执行模块603每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果,包括:The return execution module 603 uses the output of the latest executed step as the result of executing the recursive algorithm for the current variable whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is completed, including:

每当本次执行递归算法所记录的步骤标识表示所述递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;

所述返回执行模块603基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法,包括:The return execution module 603 continues to execute the last execution of the recursive algorithm based on the obtained result, starting from the execution progress of the latest record of the last execution of the recursive algorithm, including:

基于所得到的结果,从上一次执行递归算法时最新记录的步骤标识所表示的步骤开始,继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the step indicated by the step identifier most recently recorded when the recursive algorithm was executed last time.

所述步骤标识用于表示距离所述递归算法中最后一个步骤所剩余的步骤数;The step identifier is used to indicate the number of steps remaining from the last step in the recursive algorithm;

所述执行进度记录模块602每当本地执行递归算法所记录的步骤标识表示所述递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果,包括:Whenever the step identifier recorded by the locally executed recursive algorithm indicates the last step of the recursive algorithm, the execution progress recording module 602 uses the output of the latest executed step as the result of executing the recursive algorithm for the current variable, including:

每当所述步骤标识表示距离距离所述递归算法中最后一个步骤所剩余的步骤数为0时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果。Whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is taken as the result of executing the recursive algorithm for the current variable.

所述返回执行模块603还用于在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,确定本次执行递归算法是否为继续第一次执行递归算法;The return execution module 603 is also used to determine whether the current execution of the recursive algorithm is the first execution of the recursive algorithm whenever a result of executing the recursive algorithm for the current variable is obtained during the execution of the recursive algorithm;

所述返回执行模块603基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:The return execution module 603 continues to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm based on the obtained result, until the obtained result is the result of executing the recursive algorithm for the target variable, including:

如果本次执行递归算法不为继续第一次执行递归算法,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法;If the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm, the execution of the last execution of the recursive algorithm will continue from the execution progress of the last execution of the recursive algorithm;

如果本次执行递归算法为继续第一次执行递归算法,将得到的结果作为针对所述目标变量执行所述递归算法的结果。If the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, the obtained result is used as the result of executing the recursive algorithm for the target variable.

在一种可能的实施例中,所述返回执行模块603在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,确定本次执行递归算法是否为继续第一次执行递归算法,包括:In a possible embodiment, during the process of executing the recursive algorithm, the return execution module 603 determines whether the current execution of the recursive algorithm is the first execution of the recursive algorithm whenever a result of executing the recursive algorithm for the current variable is obtained, including:

在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,删除本次执行递归算法记录的执行进度;During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted;

确定是否还记录有执行进度;Determine whether there is any execution progress recorded;

如果还记录有执行进度,确定本次执行递归算法不为继续第一次执行递归算法;If the execution progress is still recorded, it is determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm;

如果没有记录执行进度,确定本次执行递归算法为继续第一次执行递归算法。If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.

在一种可能的实施例中,所述执行进度记录模块602还用于在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录当前变量,作为本次执行递归算法的输入参量;In a possible embodiment, the execution progress recording module 602 is further used to record the current variable as an input parameter of the current execution of the recursive algorithm whenever the executed step is a recursive step during the execution of the recursive algorithm;

所述返回执行模块从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,包括:The return execution module continues to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm, including:

针对上一次执行递归算法的输入参量,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法。For the input parameters of the last recursive algorithm, continue to execute the last recursive algorithm from the execution progress of the last recursive algorithm.

本发明实施例还提供了一种电子设备,如图7所示,包括处:The embodiment of the present invention further provides an electronic device, as shown in FIG7 , including:

存储器701,用于存放计算机程序;Memory 701, used for storing computer programs;

处理器702,用于执行存储器701上所存放的程序时,实现如下步骤:The processor 702 is used to execute the program stored in the memory 701, and implements the following steps:

针对当前变量,开始执行递归算法,所述当前变量的初始值为目标变量;For the current variable, a recursive algorithm is started, wherein the initial value of the current variable is the target variable;

在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,所述递归步骤为需要调用所述递归算法的步骤;During the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, and the recursive step is the step that needs to call the recursive algorithm;

在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,所述上一次执行递归算法为针对上一个当前变量执行的递归算法。During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm continues from the execution progress of the last execution of the recursive algorithm until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.

在一种可能的实施例中,所述每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,包括:In a possible embodiment, whenever the executed step is a recursive step, recording the execution progress of the recursive algorithm this time includes:

每当执行一个步骤时,记录本次执行递归算法的执行进度;Every time a step is executed, the progress of the recursive algorithm is recorded;

所述每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:Whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including:

每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;

基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm.

在一种可能的实施例中,所述记录执行递归算法的执行进度,包括:In a possible embodiment, recording the execution progress of the recursive algorithm includes:

记录所执行的步骤的步骤标识;The step ID that records the steps performed;

所述每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果,包括:Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is completed, the output of the latest executed step is used as the result of executing the recursive algorithm for the current variable, including:

每当本次执行递归算法所记录的步骤标识表示所述递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;

所述基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法,包括:The method of continuing to execute the last recursive algorithm based on the obtained result starting from the execution progress of the latest record of the last recursive algorithm execution includes:

基于所得到的结果,从上一次执行递归算法时最新记录的步骤标识所表示的步骤开始,继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the step indicated by the step identifier most recently recorded when the recursive algorithm was executed last time.

在一种可能的实施例中,所述步骤标识用于表示距离所述递归算法中最后一个步骤所剩余的步骤数;In a possible embodiment, the step identifier is used to indicate the number of steps remaining from the last step in the recursive algorithm;

所述每当本地执行递归算法所记录的步骤标识表示所述递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果,包括:Whenever the step identifier recorded by the local execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable, including:

每当所述步骤标识表示距离距离所述递归算法中最后一个步骤所剩余的步骤数为0时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果。Whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is taken as the result of executing the recursive algorithm for the current variable.

在一种可能的实施例中,所述方法还包括:In a possible embodiment, the method further includes:

在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,确定本次执行递归算法是否为继续第一次执行递归算法;During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm;

所述基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:The step of continuing to execute the last execution of the recursive algorithm based on the obtained result from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, includes:

如果本次执行递归算法不为继续第一次执行递归算法,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法;If the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm, the execution of the last execution of the recursive algorithm will continue from the execution progress of the last execution of the recursive algorithm;

如果本次执行递归算法为继续第一次执行递归算法,将得到的结果作为针对所述目标变量执行所述递归算法的结果。If the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, the obtained result is used as the result of executing the recursive algorithm for the target variable.

在一种可能的实施例中,所述在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,确定本次执行递归算法是否为继续第一次执行递归算法,包括:In a possible embodiment, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm includes:

在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,删除本次执行递归算法记录的执行进度;During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted;

确定是否还记录有执行进度;Determine whether there is any execution progress recorded;

如果还记录有执行进度,确定本次执行递归算法不为继续第一次执行递归算法;If the execution progress is still recorded, it is determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm;

如果没有记录执行进度,确定本次执行递归算法为继续第一次执行递归算法。If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.

在一种可能的实施例中,所述方法还包括:In a possible embodiment, the method further includes:

在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录当前变量,作为本次执行递归算法的输入参量;During the execution of the recursive algorithm, whenever the executed step is a recursive step, the current variable is recorded as the input parameter of this execution of the recursive algorithm;

所述从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,包括:The step of continuing to execute the last recursive algorithm from the execution progress of the last recursive algorithm includes:

针对上一次执行递归算法的输入参量,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法。For the input parameters of the last recursive algorithm, continue to execute the last recursive algorithm from the execution progress of the last recursive algorithm.

上述电子设备提到的存储器可以包括随机存取存储器(Random Access Memory,RAM),也可以包括非易失性存储器(Non-Volatile Memory,NVM),例如至少一个磁盘存储器。可选的,存储器还可以是至少一个位于远离前述处理器的存储装置。The memory mentioned in the above electronic device may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the above processor.

上述的处理器可以是通用处理器,包括中央处理器(Central Processing Unit,CPU)、网络处理器(Network Processor,NP)等;还可以是数字信号处理器(Digital SignalProcessing,DSP)、专用集成电路(Application Specific Integrated Circuit,ASIC)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件。The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

在本发明提供的又一实施例中,还提供了一种计算机可读存储介质,该计算机可读存储介质中存储有指令,当其在计算机上运行时,使得计算机执行上述实施例中任一递归函数执行方法。In another embodiment of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium stores instructions. When the instructions are executed on a computer, the computer executes any recursive function execution method in the above embodiments.

在本发明提供的又一实施例中,还提供了一种包含指令的计算机程序产品,当其在计算机上运行时,使得计算机执行上述实施例中任一递归函数执行方法。In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is run on a computer, the computer executes any recursive function execution method in the above embodiments.

在上述实施例中,可以全部或部分地通过软件、硬件、固件或者其任意组合来实现。当使用软件实现时,可以全部或部分地以计算机程序产品的形式实现。所述计算机程序产品包括一个或多个计算机指令。在计算机上加载和执行所述计算机程序指令时,全部或部分地产生按照本发明实施例所述的流程或功能。所述计算机可以是通用计算机、专用计算机、计算机网络、或者其他可编程装置。所述计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,所述计算机指令可以从一个网站站点、计算机、服务器或数据中心通过有线(例如同轴电缆、光纤、数字用户线(DSL))或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集成的服务器、数据中心等数据存储设备。所述可用介质可以是磁性介质,(例如,软盘、硬盘、磁带)、光介质(例如,DVD)、或者半导体介质(例如固态硬盘Solid State Disk(SSD))等。In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.

需要说明的是,在本文中,诸如第一和第二等之类的关系术语仅仅用来将一个实体或者操作与另一个实体或操作区分开来,而不一定要求或者暗示这些实体或操作之间存在任何这种实际的关系或者顺序。而且,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、物品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、物品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的过程、方法、物品或者设备中还存在另外的相同要素。It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

本说明书中的各个实施例均采用相关的方式描述,各个实施例之间相同相似的部分互相参见即可,每个实施例重点说明的都是与其他实施例的不同之处。尤其,对于装置、电子设备、计算机可读存储介质以及计算机程序产品的施例而言,由于其基本相似于方法实施例,所以描述的比较简单,相关之处参见方法实施例的部分说明即可。Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the embodiments of the device, electronic device, computer-readable storage medium, and computer program product, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.

以上所述仅为本发明的较佳实施例而已,并非用于限定本发明的保护范围。凡在本发明的精神和原则之内所作的任何修改、等同替换、改进等,均包含在本发明的保护范围内。The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.

Claims (9)

1.一种递归算法实现方法,其特征在于,所述方法包括:1. A recursive algorithm implementation method, characterized in that the method comprises: 针对当前变量,开始执行递归算法,所述当前变量的初始值为目标变量;For the current variable, a recursive algorithm is started, wherein the initial value of the current variable is the target variable; 在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,所述递归步骤为需要调用所述递归算法的步骤;During the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, and the recursive step is the step that needs to call the recursive algorithm; 在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,所述上一次执行递归算法为针对上一个当前变量执行的递归算法;During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm, until the obtained result is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable; 所述方法还包括:The method further comprises: 确定待执行的递归算法中调用递归算法的次数;Determine the number of times the recursive algorithm is called in the recursive algorithm to be executed; 根据递归算法,生成所述次数个递归子函数和一个非递归子函数;According to the recursive algorithm, generate the number of recursive sub-functions and a non-recursive sub-function; 对第一次迭代操作的对象创建结构体,并将创建的结构体记录于预设数组的末尾;Create a structure for the object of the first iteration operation and record the created structure at the end of the preset array; 读取所述预设数组中末尾的元素;Read the last element in the preset array; 确定执行进度是否等于0,如果所述执行进度不等于0,执行进入步骤(所述次数+2-所述执行进度),如果所述执行进度等于0,执行汇总之前各次执行递归算法的结果计算本次执行递归算法的结果;Determine whether the execution progress is equal to 0. If the execution progress is not equal to 0, execute to enter step (the number of times + 2-the execution progress). If the execution progress is equal to 0, execute to summarize the results of the previous executions of the recursive algorithm to calculate the result of the current execution of the recursive algorithm. 所述执行进度自减一;The execution progress is decremented by one; 确定所进入的子函数是否为递归子函数,如果为递归子函数,执行对下一次执行递归算法所针对的变量创建结构体,并将创建的结构体记录于所述预设数组的末尾,如果不为递归子函数,返回所述读取所述预设数组中末尾的元素;Determine whether the entered sub-function is a recursive sub-function. If it is a recursive sub-function, create a structure for the variable targeted by the next execution of the recursive algorithm, and record the created structure at the end of the preset array. If it is not a recursive sub-function, return the element at the end of the preset array; 对下一次执行递归算法所针对的变量创建结构体,并将创建的结构体记录于所述预设数组的末尾;Creating a structure for the variable targeted by the next execution of the recursive algorithm, and recording the created structure at the end of the preset array; 执行所述汇总之前各次执行递归算法的结果计算本次执行递归算法的结果;Calculate the result of the current execution of the recursive algorithm by summarizing the results of the previous executions of the recursive algorithm; 删除所述预设数组中末尾的元素;Deleting the last element in the preset array; 判断所述预设数组是否为空,如果所述预设数组不为空,返回执行所述读取所述预设数组中末尾的元素,如果所述预设数组为空,执行将最后一个得到的结果作为针对目标变量执行递归算法的结果;Determine whether the preset array is empty. If the preset array is not empty, return to execute the reading of the last element in the preset array. If the preset array is empty, execute the last obtained result as the result of executing the recursive algorithm for the target variable; 所述每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,包括:Whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, including: 每当执行一个步骤时,记录本次执行递归算法的执行进度;Every time a step is executed, the progress of the recursive algorithm is recorded; 所述每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:Whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including: 每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable; 基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm. 2.根据权利要求1所述的方法,其特征在于,所述记录执行递归算法的执行进度,包括:2. The method according to claim 1, characterized in that the recording of the execution progress of the recursive algorithm comprises: 记录所执行的步骤的步骤标识;The step ID that records the steps performed; 所述每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果,包括:Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is completed, the output of the latest executed step is used as the result of executing the recursive algorithm for the current variable, including: 每当本次执行递归算法所记录的步骤标识表示所述递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable; 所述基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法,包括:The method of continuing to execute the last recursive algorithm based on the obtained result starting from the execution progress of the latest record of the last recursive algorithm execution includes: 基于所得到的结果,从上一次执行递归算法时最新记录的步骤标识所表示的步骤开始,继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the step indicated by the step identifier most recently recorded when the recursive algorithm was executed last time. 3.根据权利要求2所述的方法,其特征在于,所述步骤标识用于表示距离所述递归算法中最后一个步骤所剩余的步骤数;3. The method according to claim 2, characterized in that the step identifier is used to indicate the number of steps remaining from the last step in the recursive algorithm; 所述每当本次执行递归算法所记录的步骤标识表示所述递归算法的最后一个步骤时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果,包括:Whenever the step identifier recorded by the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable, including: 每当所述步骤标识表示距离距离所述递归算法中最后一个步骤所剩余的步骤数为0时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果。Whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is taken as the result of executing the recursive algorithm for the current variable. 4.根据权利要求1所述的方法,其特征在于,所述方法还包括:4. The method according to claim 1, characterized in that the method further comprises: 在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,确定本次执行递归算法是否为继续第一次执行递归算法;During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm; 所述基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:The step of continuing to execute the last execution of the recursive algorithm based on the obtained result from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, includes: 如果本次执行递归算法不为继续第一次执行递归算法,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法;If the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm, the execution of the last execution of the recursive algorithm will continue from the execution progress of the last execution of the recursive algorithm; 如果本次执行递归算法为继续第一次执行递归算法,将得到的结果作为针对所述目标变量执行所述递归算法的结果。If the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, the obtained result is used as the result of executing the recursive algorithm for the target variable. 5.根据权利要求4所述的方法,其特征在于,所述在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,确定本次执行递归算法是否为继续第一次执行递归算法,包括:5. The method according to claim 4, characterized in that, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for a current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm comprises: 在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,删除本次执行递归算法记录的执行进度;During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted; 确定是否还记录有执行进度;Determine whether there is any execution progress recorded; 如果还记录有执行进度,确定本次执行递归算法不为继续第一次执行递归算法;If the execution progress is still recorded, it is determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm; 如果没有记录执行进度,确定本次执行递归算法为继续第一次执行递归算法。If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm. 6.根据权利要求1所述的方法,其特征在于,所述方法还包括:6. The method according to claim 1, characterized in that the method further comprises: 在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录当前变量,作为本次执行递归算法的输入参量;During the execution of the recursive algorithm, whenever the executed step is a recursive step, the current variable is recorded as the input parameter of this execution of the recursive algorithm; 所述从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,包括:The step of continuing to execute the last recursive algorithm from the execution progress of the last recursive algorithm includes: 针对上一次执行递归算法的输入参量,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法。For the input parameters of the last recursive algorithm, continue to execute the last recursive algorithm from the execution progress of the last recursive algorithm. 7.一种递归算法实现装置,其特征在于,所述装置包括:7. A recursive algorithm implementation device, characterized in that the device comprises: 递归执行模块,用于针对当前变量,开始执行递归算法,所述当前变量的初始值为目标变量;A recursive execution module, used to start executing a recursive algorithm for a current variable, wherein the initial value of the current variable is a target variable; 执行进度记录模块,用于在执行递归算法过程中,每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,所述递归步骤为需要调用所述递归算法的步骤;An execution progress recording module is used to record the execution progress of the recursive algorithm in the process of executing the recursive algorithm, whenever the executed step is a recursive step, and the recursive step is a step that needs to call the recursive algorithm; 返回执行模块,用于在执行递归算法过程中,每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,所述上一次执行递归算法为针对上一个当前变量执行的递归算法;The return execution module is used for, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, continuing to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm, until the obtained result is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable; 所述装置还用于:The device is also used for: 确定待执行的递归算法中调用递归算法的次数;Determine the number of times the recursive algorithm is called in the recursive algorithm to be executed; 根据递归算法,生成所述次数个递归子函数和一个非递归子函数;According to the recursive algorithm, generate the number of recursive sub-functions and a non-recursive sub-function; 对第一次迭代操作的对象创建结构体,并将创建的结构体记录于预设数组的末尾;Create a structure for the object of the first iteration operation and record the created structure at the end of the preset array; 读取所述预设数组中末尾的元素;Read the last element in the preset array; 确定执行进度是否等于0,如果所述执行进度不等于0,执行进入步骤(所述次数+2-所述执行进度),如果所述执行进度等于0,执行汇总之前各次执行递归算法的结果计算本次执行递归算法的结果;Determine whether the execution progress is equal to 0. If the execution progress is not equal to 0, execute to enter step (the number of times + 2-the execution progress). If the execution progress is equal to 0, execute to summarize the results of the previous executions of the recursive algorithm to calculate the result of the current execution of the recursive algorithm. 所述执行进度自减一;The execution progress is decremented by one; 确定所进入的子函数是否为递归子函数,如果为递归子函数,执行对下一次执行递归算法所针对的变量创建结构体,并将创建的结构体记录于所述预设数组的末尾,如果不为递归子函数,返回所述读取所述预设数组中末尾的元素;Determine whether the entered sub-function is a recursive sub-function. If it is a recursive sub-function, create a structure for the variable targeted by the next execution of the recursive algorithm, and record the created structure at the end of the preset array. If it is not a recursive sub-function, return the element at the end of the preset array; 对下一次执行递归算法所针对的变量创建结构体,并将创建的结构体记录于所述预设数组的末尾;Creating a structure for the variable targeted by the next execution of the recursive algorithm, and recording the created structure at the end of the preset array; 执行所述汇总之前各次执行递归算法的结果计算本次执行递归算法的结果;Calculate the result of the current execution of the recursive algorithm by summarizing the results of the previous executions of the recursive algorithm; 删除所述预设数组中末尾的元素;Deleting the last element in the preset array; 判断所述预设数组是否为空,如果所述预设数组不为空,返回执行所述读取所述预设数组中末尾的元素,如果所述预设数组为空,执行将最后一个得到的结果作为针对目标变量执行递归算法的结果;Determine whether the preset array is empty. If the preset array is not empty, return to execute the reading of the last element in the preset array. If the preset array is empty, execute the last obtained result as the result of executing the recursive algorithm for the target variable; 所述执行进度记录模块每当所执行的步骤为递归步骤时,记录本次执行递归算法的执行进度,包括:The execution progress recording module records the execution progress of the recursive algorithm whenever the executed step is a recursive step, including: 每当执行一个步骤时,记录本次执行递归算法的执行进度;Every time a step is executed, the progress of the recursive algorithm is recorded; 所述返回执行模块每当得到针对当前变量执行所述递归算法的结果时,基于所得到的结果,从上一次执行递归算法的执行进度开始继续执行上一次执行递归算法,直至所得到的结果为针对所述目标变量执行所述递归算法的结果,包括:Whenever the return execution module obtains a result of executing the recursive algorithm for the current variable, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including: 每当本次执行递归算法的执行进度表示本次执行递归算法完成时,将最新执行的步骤的输出作为针对当前变量执行所述递归算法的结果;Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable; 基于所得到的结果,从上一次执行递归算法的最新记录的执行进度开始继续执行上一次执行递归算法。Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm. 8.一种电子设备,其特征在于,包括:8. An electronic device, comprising: 存储器,用于存放计算机程序;Memory, used to store computer programs; 处理器,用于执行存储器上所存放的程序时,实现权利要求1-6任一所述的方法步骤。A processor, for implementing the method steps described in any one of claims 1 to 6 when executing a program stored in a memory. 9.一种计算机可读存储介质,其特征在于,所述计算机可读存储介质内存储有计算机程序,所述计算机程序被处理器执行时实现权利要求1-6任一所述的方法步骤。9. A computer-readable storage medium, characterized in that a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, the method steps described in any one of claims 1 to 6 are implemented.
CN202010608367.7A 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment Active CN111767522B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010608367.7A CN111767522B (en) 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010608367.7A CN111767522B (en) 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment

Publications (2)

Publication Number Publication Date
CN111767522A CN111767522A (en) 2020-10-13
CN111767522B true CN111767522B (en) 2024-11-01

Family

ID=72724253

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010608367.7A Active CN111767522B (en) 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment

Country Status (1)

Country Link
CN (1) CN111767522B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112508228A (en) * 2020-11-03 2021-03-16 北京理工大学前沿技术研究院 Driving behavior risk prediction method and system

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110347180A (en) * 2019-08-12 2019-10-18 南京邮电大学 The method for calculating the most short tail clearance that unmanned plane cluster is formed into columns again
CN111258782A (en) * 2020-01-17 2020-06-09 北京海益同展信息科技有限公司 Processing method and device of task queue

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9003371B2 (en) * 2010-12-30 2015-04-07 International Business Machines Corporation Recursive method call representation in a plot view of method execution performance
US9720966B2 (en) * 2012-12-20 2017-08-01 Teradata Us, Inc. Cardinality estimation for optimization of recursive or iterative database queries by databases

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110347180A (en) * 2019-08-12 2019-10-18 南京邮电大学 The method for calculating the most short tail clearance that unmanned plane cluster is formed into columns again
CN111258782A (en) * 2020-01-17 2020-06-09 北京海益同展信息科技有限公司 Processing method and device of task queue

Also Published As

Publication number Publication date
CN111767522A (en) 2020-10-13

Similar Documents

Publication Publication Date Title
KR102226257B1 (en) Method and device for writing service data to a blockchain system
CN108228649B (en) Method and apparatus for data access
CN108228646B (en) Method and electronic device for accessing data
CN111767522B (en) Recursive algorithm implementation method and device and electronic equipment
WO2024001025A1 (en) Pre-execution cache data cleaning method and blockchain node
CN112631682B (en) Mini-program processing method, device, equipment and storage medium
CN109240893B (en) Application running state query method and terminal equipment
WO2021174763A1 (en) Database management method and apparatus based on lookup table
CN111159160A (en) A version rollback method, device, electronic device and storage medium
CN108763517A (en) A kind of method and relevant device for deleting metadata
KR101443942B1 (en) Distributed search in casual network of server
CN117556086B (en) A multi-hop path query method, device, computer equipment and storage medium
TWI710918B (en) An optimization method, device and computer equipment of LSM tree
CN109615465B (en) Service order processing method and device and electronic equipment
CN110413215B (en) Method, apparatus and computer program product for obtaining access rights
CN115865839B (en) ACL management method, device, communication equipment and storage medium
WO2024146415A1 (en) Task retrieval method and apparatus, and electronic device
CN111666302A (en) User ranking query method, device, equipment and storage medium
CN117057277A (en) Calculation task processing method and device based on lattice Boltzmann model
CN110990240B (en) Method, device and medium for predicting performance of storage system
CN109992695B (en) Video information query method and device
CN115202823A (en) Cloud platform management method and related equipment
CN113849482A (en) Data migration method and device and electronic equipment
CN109739812A (en) Method and device for displaying resource files
CN116737400B (en) Queue data processing method and device and related equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant