程序中递归和非递归的效率_C程序中的递归

程序中递归和非递归的效率

The recursion is a technique of programming in C and various other high-level languages in which a particular function calls itself either in a direct or indirect manner. The use of recursive algorithm can make certain complex programming problems to be solved with ease.

递归是用C语言和其他各种高级语言编程的技术,其中特定函数以直接或间接方式调用自身。 递归算法的使用可以使某些复杂的编程问题轻松解决。

How to go about solving problems involving recursion?

如何解决涉及递归的问题?

Recursion algorithms can sometimes turn out to be difficult to understand for beginners and intermediate programmers. However, a particular approach to recursive problems can simplify the process by a great margin.

对于初学者和中级程序员而言,有时有时很难理解递归算法。 但是,解决递归问题的特定方法可以大大简化该过程。

1) You must apply your ideas with a recursive approach to the problems involving recursion.

1)您必须以递归的方式将您的想法应用于涉及递归的问题。

2) Keep in mind most of the recursive problems have two cases:

2)请记住,大多数递归问题有两种情况:

2.1) Base Case:
The base case is the simplified form of the problem, which has no further scope of expression into its own terms i.e. the function is no more called and the recursion terminates. Because of this, it is also referred to as the termination condition. The base case can be omitted if you desire to form an infinite recursion.

2.1) 基本案例:
基本情况是问题的简化形式,它没有进一步的表达范围用自己的术语表示,即不再调用函数并且递归终止。 因此,它也称为终止条件。 如果希望形成无限递归,则可以省略基本情况。

int fact(int n)
{
	if (n <= 1) // base case
		return 1;
	else    
		return n*fact(n-1);   
}

For example, in the above code snippet (n<=1) is the base case for the function fact.

例如,在上面的代码片段中(n <= 1)是函数事实的基本情况。

2.2) Recursive Case:
In recursive case, there is further scope for the problem to be defined in terms of itself, which in turn reduces the problem size.

2.2) 递归的情况:
在递归的情况下,根据问题本身来定义问题还有更大的范围,从而减小了问题的大小。

3) Now that you know what Base and Recursive cases are, next step is expressing your problem in the form of these two cases.

3)现在您知道了什么是基本案例和递归案例,下一步就是以这两种案例的形式来表达您的问题。

fact(n) = {1}
{n*fact(n-1)}

4) Now that you have the two cases with you, last step is to simply code for the recurrence relation.

4)现在您有了这两种情况,最后一步是简单地为递归关系编写代码。

直接和间接递归技术 (Direct and Indirect Recursion Techniques)

Direct recursion is simply the function calling itself i.e. the function body contains an explicit call to itself. On the other hand, Indirect recursion occurs when a function body calls a different function which in turn calls another function, ultimately resulting in the original function being called in the process. The functions involved in indirect recursion are called mutually recursive functions. Comparatively, it is simpler and safer to use direct recursion as it is much easier to understand and apply.

直接递归只是函数调用自身,即函数主体包含对自身的显式调用。 另一方面,当函数主体调用不同的函数又调用另一个函数时,就会发生间接递归,最终导致原始函数在过程中被调用。 间接递归涉及的函数称为互递归函数。 相比之下,使用直接递归更为简单和安全,因为它更易于理解和应用。

Example to solve recursion problems,

解决递归问题的示例,

The below program gives an illustration of finding the factorial of a number using recursion in C.

下面的程序说明了如何使用C中的递归查找数字的阶乘。

#include <stdio.h>

unsigned long long int factorial(unsigned int i) 
{
	if(i<= 1) 
	{
		return 1;
	}
	return i * factorial(i - 1);
}

int  main() 
{
	int i = 12;
	
	printf("Factorial of %d is %ld\n", i, factorial(i));
	
	return 0;
}

Output

输出量

    Factorial of 12 is 479001600

递归编程与迭代编程 (Recursive Programming Vs Iterative Programming)

Recursive programming as discussed earlier involves recursive function whereas in Iterative programming we use loops ( some commonly used loops are ‘for’, ‘while’, do-while loops).

如前所述,递归编程涉及递归函数,而在迭代编程中,我们使用循环(一些常用的循环为“ for”,“ while”,“ do-while”循环)。

Now, let's look at the difference between the two.

现在,让我们看一下两者之间的区别。

Recursive ProgrammingIterative Programming
Recursive program has greater space requirements.Iterative programming consumes less space
It has greater time requirements for operation because of function calls and return overhead.They operate faster than recursive functions.
Recursive functions can be solved iteratively.Iterative functions can be solved recursively.
Inherently recursive program include Tower of Hanoi problem.Simple iterative program include finding sum of first n numbers.
递归编程 迭代编程
递归程序具有更大的空间要求。 迭代编程消耗更少的空间
由于函数调用和返回开销,它对操作的时间要求更大。 它们比递归函数运行得更快。
递归函数可以迭代求解。 迭代函数可以递归求解。
固有的递归程序包括河内塔问题。 简单的迭代程序包括查找前n个数字的和。

翻译自: https://www.includehelp.com/c/recursion.aspx

程序中递归和非递归的效率

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值