在 C# 中递归检索数组中的最大数字?

发布于 2024-10-07 13:26:32 字数 28 浏览 4 评论 0原文

如何在 C# 中递归检索数组中的最大数字?

How can I retrieve the highest number in an array recursively in C#?

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

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

发布评论

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

评论(3

萌无敌 2024-10-14 13:26:32

现在你可能会认为我们没有给你答案是很卑鄙的——我承认我已经写下了答案,甚至我也想把它给你。

编程就是自己寻找问题的解决方案。当你被聘为程序员时,你可能会有其他人可以依靠,但他们都有自己的问题,你需要能够承担自己的责任。

递归(用过于简单的答案来说)意味着一遍又一遍地调用相同的操作,直到产生结果。这意味着您需要在每个递归操作中,您需要(至少)知道两件事:

  1. 您正在寻找什么
  2. 您到目前为止发现了什么

“您正在寻找什么”是终止条件。一旦你发现了这一点,所有的工作就可以停止,你就可以回家了。

“到目前为止你发现了什么”是指你如何知道你已经检查过什么,这样你就不会重蹈覆辙。

那么,为了递归地找到数组中的最高值,您需要知道什么?

  1. 数组的内容。
  2. 迄今为止您发现的最高数字。
  3. 您已经看过数组的这一部分了吗? (为什么要再次查看它?)

这会产生一个如下所示的方法签名:

public int GetHighestNumber(int[] array, int highestNumberFound, int lastIndexChecked);

一旦进入数组,您必须执行以下操作:

  1. 迭代数组
  2. 当您发现一个值高于highestNumberFound
  3. 再次调用 GetHighestNumber,并更新新的 highestNumberFoundlastIndexChecked
  4. 当没有更多“更高”数字时,返回找到的最大数字。

我意识到这听起来很老套,但是自己学习这些东西会让你成为一个更好的程序员。

如果你想成为一名专业程序员,你就必须自学这些东西。

如果你不想成为一名专业程序员,那就放弃课程,做一些你喜欢的事情。

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:

  1. What you're looking for
  2. What you've found so far

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?

  1. The contents of the Array.
  2. The highest number you've found so far.
  3. Have you already looked at this part of the Array? (Why look through it again?)

That would produce a method signature that looks like:

public int GetHighestNumber(int[] array, int highestNumberFound, int lastIndexChecked);

Once you're inside the array, you've got to do the following:

  1. Iterate through the array
  2. Stop when you find a value that is higher than the highestNumberFound
  3. Call GetHighestNumber again with the new highestNumberFound and lastIndexChecked updated.
  4. When there are no more 'higher' numbers, then return the highest number found.

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.

人疚 2024-10-14 13:26:32

这里只是一个提示(以 int[] 为例):

public int FindMax(int[] array, int indexSoFar, int maxSoFar)

思考:

  • 开始条件
  • 终止条件
  • 如何递归地遍历数组

Here's just a hint (taking int[] as an example):

public int FindMax(int[] array, int indexSoFar, int maxSoFar)

Think about:

  • The start conditions
  • The termination conditions
  • How you move through the array recursively
笑脸一如从前 2024-10-14 13:26:32

编辑原因:不想破坏答案。
问候。

Reason of EDIT: Didnt want to spoil the answere.
Greetings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文