适合初学者的递归函数?
我正在寻找一些递归函数示例,最好是那些显示复杂性增加的示例。我了解基本的递归函数,但在代码中实现它们时遇到问题。我以前从未在我的代码中使用过它们,而且我知道它并不总是需要它,但我想尝试一下。是否有很好的资源,其中包含示例以及某种挑战?或者甚至是一些我可以使用递归的简单数学问题(欧拉项目?)?
例如,我更喜欢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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!
我发现斯坦福工程无处不在项目提供的在线课程是一个极好的资源。如果你看一下他们的 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
好吧,如果您在实现基本递归函数时遇到困难,我建议您实际上并不理解它们。但这并不丢脸,并不是每个人都能轻易“掌握”递归。我建议您不要去尝试简单的算法:
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: