适合初学者的递归函数?

发布于 2024-08-28 12:07:09 字数 182 浏览 3 评论 0原文

我正在寻找一些递归函数示例,最好是那些显示复杂性增加的示例。我了解基本的递归函数,但在代码中实现它们时遇到问题。我以前从未在我的代码中使用过它们,而且我知道它并不总是需要它,但我想尝试一下。是否有很好的资源,其中包含示例以及某种挑战?或者甚至是一些我可以使用递归的简单数学问题(欧拉项目?)?

例如,我更喜欢 C#,但任何东西都可以。

I'm looking for a few recursive function examples, preferably ones that show increase in complexity. I understand basic recursive functions, but I'm having trouble implementing them in my code. I've never used them in my code before, and I know it doesn't always call for it, but I'd like to try. Is there a good resource with examples, and maybe challenges of some sort? Or even some simple math problems (Project Euler-style?) that I could use recursion on?

For examples, I prefer C#, but anything works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十六岁半 2024-09-04 12:07:09

1 最简单的是阶乘、斐波那契序列或由递归函数定义的任何数学序列。

2 然后您可以转向

使用深度优先搜索的任何算法。

例如树遍历图遍历

搜索问题,如8 皇后问题

3 你可能想学习
分治算法,例如归并排序和快速排序。它们通常是递归实现的。

这些都非常经典!

1 the easiest ones are factorial, Fibonacci sequences, or any mathematical sequence defined by recursive functions.

2 then you can move to

any algorithms that use a depth first search.

E.g. tree traversal, graph traversal,

search problems, like 8-queens problems.

3 you'd probably like to study
divide and conquer algorithm, e.g. merge sort and quick sort. they are usually implemented recursively.

These are all very classical!

顾挽 2024-09-04 12:07:09

我发现斯坦福工程无处不在项目提供的在线课程是一个极好的资源。如果你看一下他们的 CS106B 课程,第 7 到 11 讲,你应该对递归有一个很好的基本了解。他们还提供需要解决的问题的练习。

http://see.stanford.edu/请参阅/lecturelist.aspx?coll=11f4f422-5670-4b4c-889c-008262e09e4e

I found the online courses made available by the Stanford Engineering Everywhere program to be an excellent resource. If you look at their CS106B course, lectures 7 through 11, you should have a good basic understanding of recursion. They also provide exercises with problems to solve.

http://see.stanford.edu/see/lecturelist.aspx?coll=11f4f422-5670-4b4c-889c-008262e09e4e

攒眉千度 2024-09-04 12:07:09

好吧,如果您在实现基本递归函数时遇到困难,我建议您实际上并不理解它们。但这并不丢脸,并不是每个人都能轻易“掌握”递归。我建议您不要去尝试简单的算法:

  • 思考并研究递归数据结构(例如树、图或列表)的用途。在递归是自然方法的情况下学习使用递归可能是获得递归乐趣的更好途径,而不是尝试大量实际上应该用循环来解决的示例。
  • 使用递归是基本、原始概念的语言。 Haskell 和其他一些函数式编程语言就可以了。通常,解决 FP 问题最自然的方法是实现递归;在其他语言中,您通常必须首先与该语言奇怪的递归方法作斗争。我不知道 C# 是否有一种奇怪的递归方法。

Well, if you are having trouble implementing basic recursive functions, I'd suggest that you don't actually understand them. But there's no shame in that, not everyone 'gets' recursion readily. Rather than butt your head against the simple algorithms I'd suggest you try:

  • Thinking about, and studying the uses of, recursive data structures such as trees, graphs or lists. Learning to use recursion in situations where recursion is the natural approach is probably a better route to recursive joy than trying lots of examples which ought, really, to be tackled with loops.
  • Using a language in which recursion is a basic, primitive, concept. Haskell and some of the other functional programming languages would do. Oft-times the most natural way to tackle a problem with FP is to implement recursion; in other languages you often have to struggle first with the language's weird approach to recursion. Whether C# has a weird approach to recursion I do not know.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文