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

问题描述

我无法思考/递归的角度解决问题。我真的AP preciate的概念,我可以理解他们像创建基本情况,出口情况和放大器;递归调用等等。我可以解决这样写的阶乘或整体的总和,在数组中的简单问题。这就是我的思维停止。我无法真正应用的概念,或拿出解决方案时,问题就变得复杂。例如,汉诺塔,虽然我能理解的问题和解决方案,我,我自己不能与一个解决方案。它适用于其他算法,如快速排序/二叉树的遍历为好。所以我的问题是

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. 将学习函数式语言帮助我理解?

请指教。

推荐答案

递归仅仅是一种思维方式,就如同反复的。当我们还是孩子,在学校里,我们并没有教给递归地思考,其实存在着真正的问题。你需要将思维方式进入你的武器库,一旦你做了,它就会永远呆在那里。

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 在已经输入 N-1 的解决方案,该解决方案。

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. 了解有关树的数据结构:这尤其是国际海事组织最好的训练。树是递归的数据结构。了解他们的遍历(先序,后序,preorder,计算出其高度,直径,等)。几乎在一个树状的数据结构每一个操作是一个很好的锻炼。
  4. 组合问题:非常重要,组合,排列等
  5. 寻路:利的算法,迷宫算法等
  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.

使用一个简单的编程语言,一个你最熟悉的,preferably一个不忙你的心有很多烦恼的内存和指针。 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.

要的主递归的,你首先需要的主递归的:)

To master recursion, you need first master recursion :)

希望这有助于!

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

08-14 03:21