如何以递归方式思考? [英] How to think in recursive way?

查看:128
本文介绍了如何以递归方式思考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了理解贪婪方法和动态编程等高级算法概念,首先需要精通递归。

In order to understand the advanced algorithm concepts like greedy methods and dynamic programming, one first need to be well versed in recursion.

我对递归相对较新。每当提出问题时,首先要记住的是使用迭代的解决方案。即使我知道递归方法的含义及其工作方式,但很难以递归的方式思考。

I am relatively new to recursion. Whenever a question is asked, the first things that pops up in mind are solutions using iterations. Even though I know what recursive methods means and how it works, it is very difficult to think in a recursive way.

请回答以下问题帮助:

1)任何迭代方法都可以用递归替换,反之亦然吗?

1) Can any iterative method be replaced by recursion and vice versa?

例如,如何打印元素一个递增的大小为n的数组?

For example, how to print elements in an array of size n recursively?

for i 0 to n
 Print a[i]

2)如何递归地解决问题?步骤是什么?是否有任何提示可以确定问题可以递归解决?

2) How to solve a problem recursively? What are the steps? Is there any tips to identify that a problems can be solved recursively?

例如:如果要求打印出字符串的所有子字符串

For ex: If am asked to print out all the sub strings of a string

INPUT: CAT
OUTPUT: CAT,CA,A,AT,T

我可以快速提出迭代方法。使用两个循环可以解决问题。

I can come up with an iterative way fast.Using two loops can solve the problem.

但是递归地如何解决它。如何确定问题可以递归解决。

But recursively how to solve it.How to identify that a problems can be solved recursively.

如果回答我的第一个问题是肯定的,那么使用两次递归而不是迭代可以解决问题我的问题?

If answer to my first question is yes, using two recursions instead of iteration can be solution to my problem?

3)有人能建议我一些材料/资源来彻底理解递归的概念吗?

3) Can anyone suggest me some materials/resources to understand the concept of recursion thoroughly?

推荐答案


  1. 是的,主要是。一般来说,递归是为程序员而不是计算机进行的。迭代方法在某些情况下可能比递归方法运行得更快,但迭代方法可能需要300行代码和递归3.还有一些情况下,很容易弄清楚如何递归编程,但非常难以反复写入,反之亦然。

  1. Yes, principally. In general recursion is done for the programmer's sake, not the computer's. There are iterative methods that in some cases may run faster than recursive ones, but the iterative method may take 300 lines of code and the recursive 3. There are also some cases where it's easy to figure out how to program something recursively, but is very difficult to write iteratively and vice versa.

一般来说,递归解决方案需要在功能方面。如果我们使用类似C ++的东西,我们可以使用处理字符串引用和事物的解决方案,慢慢调整作为参数传递的字符串。接近两次递归结束时的观点是错误的。这里的原则是,我们可以做一个递归方法而不是两次迭代。

Generally the recursive solution needs to be though of in terms of the function. If we're using something like C++, we can use a solution dealing with string references and things, slowly adjusting the strings being passed as parameters. The point near the end of "two recursions" is misguided though. The principle here is that instead of two iterations, we can do one recursive approach.

http://introcs.cs.princeton.edu/java/23recursion/ 这个网站(谷歌搜索的高处)教授了很多数学理论递归,并包括一个常见问题解答,可以给你一个更令人满意的答案第一。

http://introcs.cs.princeton.edu/java/23recursion/ this site (high up on the google search) teaches a lot of the math theory of recursion and includes a FAQ which may give you a more satisfying answer to number one.

这篇关于如何以递归方式思考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆