在 C# 中递归检索数组中的最大数字?
如何在 C# 中递归检索数组中的最大数字?
How can I retrieve the highest number in an array recursively in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 C# 中递归检索数组中的最大数字?
How can I retrieve the highest number in an array recursively in C#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
现在你可能会认为我们没有给你答案是很卑鄙的——我承认我已经写下了答案,甚至我也想把它给你。
编程就是自己寻找问题的解决方案。当你被聘为程序员时,你可能会有其他人可以依靠,但他们都有自己的问题,你需要能够承担自己的责任。
递归(用过于简单的答案来说)意味着一遍又一遍地调用相同的操作,直到产生结果。这意味着您需要在每个递归操作中,您需要(至少)知道两件事:
“您正在寻找什么”是终止条件。一旦你发现了这一点,所有的工作就可以停止,你就可以回家了。
“到目前为止你发现了什么”是指你如何知道你已经检查过什么,这样你就不会重蹈覆辙。
那么,为了递归地找到数组中的最高值,您需要知道什么?
这会产生一个如下所示的方法签名:
一旦进入数组,您必须执行以下操作:
highestNumberFound
GetHighestNumber
,并更新新的highestNumberFound
和lastIndexChecked
。我意识到这听起来很老套,但是自己学习这些东西会让你成为一个更好的程序员。
如果你想成为一名专业程序员,你就必须自学这些东西。
如果你不想成为一名专业程序员,那就放弃课程,做一些你喜欢的事情。
Right now you're probably thinking that we're mean for not giving you the answer -- and I admit that I have the answer written down and part of me wants to give it to you, even.
Programming is all about finding the solutions to problems yourself. When you're hired as a programmer, you may have other people to lean on, but they've all got their own problems, and you'll need to be able to pull your own weight.
Recursion (in an oversimplifed answer) means to call the same operation over and over until the result is produced. That means you need in every recursive operation, you need to know (at least) two things:
The 'What you're looking for' is the termination condition. Once you find that, all work can stop and you can go home.
The 'what you've found so far' is how you know what've you've checked so you don't retread old ground.
So what do you need to know in order to find the highest value in an array recursively?
That would produce a method signature that looks like:
Once you're inside the array, you've got to do the following:
highestNumberFound
GetHighestNumber
again with the newhighestNumberFound
andlastIndexChecked
updated.I realize it sounds trite, but learning this stuff on your own will make you a better programmer.
If you want to be a professional programmer, you have got to learn this stuff on your own.
If you don't want to be a professional programmer, then drop the course and do something you love.
这里只是一个提示(以
int[]
为例):思考:
Here's just a hint (taking
int[]
as an example):Think about:
编辑原因:不想破坏答案。
问候。
Reason of EDIT: Didnt want to spoil the answere.
Greetings.