掌握递归编程 [英] Mastering Recursive Programming

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

问题描述

在递归方面,我在思考/解决问题时遇到麻烦。我真的非常欣赏这个概念,我可以理解他们,就像创建基本案例,退出案例&递归调用等等。我可以解决简单的问题,如在数组中写入因子或求和的整数。那就是我的思想停止了。当问题变得复杂时,我无法真正应用这些概念或提出解决方案。例如河内的塔,虽然我可以理解问题和解决方案,但我自己也不能解决问题。它适用于其他算法,如快速排序/二进制树遍历。所以我的问题是

I am having trouble in thinking/solving the problem in terms of recursion. I really appreciate the concept and I can understand them like creating base case, exit case & the recursive calls etc. I can solve simple problems like writing factorial or summation of integers in an array. That's where my thinking stops. I couldn’t really apply the concepts or come up with solutions when problem gets complicated. For instance, tower of Hanoi, though I can understand the problem and solution, I, on my own can't up with a solution. It applies to other algorithms like quick sort/binary tree traversal as well. So my question is


  1. 掌握它的最好方法是什么?

  2. 任何人都可以提出问题或问题的列表,我可以用它来练习练习吗?

  3. 学习功能语言能帮助我理解吗?

请咨询。 >

Please advice.

推荐答案

递归只是一种思考方式,就像迭代一样。当我们在学校的孩子时,我们没有被教导回想起来,这就是真正的问题。你需要将这种思维方式纳入你的武器库,一旦你这样做,它将永远留在那里。

Recursion is just a way of thinking, just as iterative is. When we were kids at school, we weren't taught to think recursively and there lies the real problem. You need to incorporate that way of thinking into your arsenal, once you do it, it'll stay there forever.

我发现有用的是首先找出基础案例,也许起初它们不是最简单的,但一旦你开始在基础案例之上构建递归,你会意识到你可以简化它。确定基本案例的重要性是,首先,您将重点放在以最简单的形式(简单的情况)需要解决的问题上,并以某种方式绘制未来算法的路线图,其次,确保算法停止。也许不会返回预期的结果,但至少停止,这总是令人鼓舞。

I found useful to always figure out the base cases first, maybe at first they aren't the most simple ones, but once you start building the recursion on top of that base case you'll realize you can simplify it. The importance of identifying the base case is that first, you focus on what needs to be solved at its simplest form (the simpler cases) and this somehow draws a road map for the future algorithm, second, you make sure the algorithm stops. Maybe doesn't return the expected result, but at least stops, which is always encouraging.

此外,它总是帮助弄清楚一个小问题的实例将如何帮助你找到一个更大的问题的实例的解决方案。例如,您如何为输入 n-1 的解决方案构建输入 n 的解决方案。

Also, it always help figuring out how a small instance of a problem will help you finding the solution of a bigger instance of the problem. This is for example, how can you build the solution for input n having already the solution of input n-1.

解决您可以递归想到的每个问题。是的,河内塔不是一个很好的例子,它的递归解决方案是一个非常聪明的解决方案。尝试更容易的问题,几乎是元素问题。

Solve every problem you can think of recursively. Yes, Hanoi Towers ain't a very good example, its recursive solutions is a very clever solution. Try easier problems, almost elemental problems.


  1. 数学运算:指数指数和您可以想到的每个数学运算。

  2. 字符串处理:回文是一个很好的练习。在网格中查找词也很有用。

  3. 了解树数据结构:这特别是IMO是最好的培训。树是递归的数据结构。了解他们的遍历(按顺序,顺序,预订,计算其高度,直径等)。几乎每一个树状数据结构的操作都是一个很好的练习。

  4. 组合问题非常重要,组合,排列等。 b $ b
  5. 路径查找: Lee的算法,迷宫算法等。

  1. Math operations: Exponentiation and every mathematical operation you can think of.
  2. String handling: Palindrome is a very good exercise. Finding words in a grid is also useful.
  3. Learn about tree data structures: This in particular is IMO the best training. Trees are recursive data structures. Learn about their traversals (inorder, postorder, preorder, calculate its height, its diameter, etc). Almost every operation on a tree-like data structure is a great exercise.
  4. Combinatorial problems: Very important, combinations, permutations, etc.
  5. Path finding: Lee's algorithm, Maze algorithms etc.

但最重要的是, 从简单的问题开始。几乎每个问题都有一个递归的解决方案。数学问题是很好的掌握它。每次您在循环或循环中看到时,请将该算法转化为递归。

But most importantly, begin with simple problems. Almost every problem have a recursive solution. Math problems are great to get a grasp of it. Every time you see a for loop or a while loop, turn that algorithm into recursion.

功能编程主要依赖于递归。我不认为这应该很有帮助,因为它们本质上是递归的,对于不了解递归的用户来说,这可能是麻烦的。

Functional programming relies heavily on recursion. I don't think that should help much since they are inherently recursive and can be cumbersome for users who don't understand recursion very much yet.

使用一种简单的编程语言,您最熟悉的程序语言,最好是不会因为内存烦恼和指针而烦恼。 Python在我看来是一个非常好的开始。很简单,不用打字或复杂的数据结构打扰你。只要这个语言可以帮助你保持专注于递归,那将会更好。

Use a simple programming language, the one you're most familiar with, preferably one that doesn't busy your mind much with memory annoyances and pointers. Python is a very good start in my opinion. Is very simple, doesn't bother you with typing or complicated data structures. As long as the language helps you stay focused only on recursion, it will be better.

最后一个建议,如果找不到问题的解决方案,搜索为了在互联网上寻求帮助或者寻求帮助,请谅解它完全是什么,然后继续前进。不要让他们绕过你,因为你想做的是将这种想法融入你的头脑

One last advice, if you can't find a solution to a problem, search for it on the internet or call for help, understand what it does completely and move on to the other. Don't let them bypass you, because what you're trying to do is incorporate that way of thinking to your head.

em> master recursion ,您需要首先执行主递归:)

To master recursion, you need first master recursion :)

希望这有帮助!

这篇关于掌握递归编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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