提取三个最小值

发布于 2024-10-22 05:38:47 字数 85 浏览 4 评论 0原文

我想获得双精度数组的最小值和其他两个最小值。总共我想要获得数组中 3 个较小的值。我没有使用类数组,但我使用的是double[]

I want to obtain the minimum of a double array and the other two minimum values. In total I wan to obtain the 3 smaller values of the array. I am not using the class array, but I am using a double[].

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

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

发布评论

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

评论(4

转身以后 2024-10-29 05:38:47

最简单的方法是调用

Arrays.sort()

并获取前 3 个值。

否则,您可以简单地循环遍历数组并跟踪三个最小的,就像您跟踪最小的一样。

Easiest way is to call

Arrays.sort()

and take the first 3 values.

Otherwise, you can simply loop through the array and keep track of the three smallest, much like you would the smallest.

淡看悲欢离合 2024-10-29 05:38:47
double[] dlist = {17.0, 10.0, 44, 7, 4.0, 33, 24, 10, 48.0, 49.0};
Arrays.sort (dlist);
System.out.println (dlist [0] + " " + dlist [1] /*...*/);
double[] dlist = {17.0, 10.0, 44, 7, 4.0, 33, 24, 10, 48.0, 49.0};
Arrays.sort (dlist);
System.out.println (dlist [0] + " " + dlist [1] /*...*/);
下雨或天晴 2024-10-29 05:38:47

与上面类似,您可以循环并存储最小的一个,然后从数组中删除。然后再做一次,再做一次。但我认为上面提到的方法是更有效的。

Similarly to above, you could loop through and store the smallest one, then remove from the array. Then do it again, and again. But I think the ways mentioned above are more efficient.

分開簡單 2024-10-29 05:38:47

好吧,如果您根本无法使用 Arrays 类,您可能需要 3 个变量,其中一个变量用于保存您尝试获取的每个值。首先将它们设置为等于数组中的前 3 个元素(如果至少有 3 个,否则只需设置其中的一些元素)。

然后使用 for 循环遍历数组中的其余元素。如果某个元素小于您已找到的一个或多个数字,请删除这些数字中最大的数字,并将其添加到最小数字列表中。

1. declare 3 variables
2. set variables equal to first 3 elements in array
3. loop from index 3 (4th element) to the length of the array
   a. see which of the already found numbers is bigger than the current element (if any)
   b.replace the biggest of the found numbers with the new number if at least one was found
4. print out or return the numbers you found

Well, if you can't use the Arrays class at all, you will probably want 3 variables, one to hold each of the values you are trying to get. Just start off by setting them equal to the first 3 elements in the array (if there are at least 3, otherwise just set a few of them).

Then use a for loop to go through the rest of the elements in the array. If an element is smaller than one or more of the numbers you already found, get rid of the largest of those numbers and add this one to the list of smallest numbers instead.

1. declare 3 variables
2. set variables equal to first 3 elements in array
3. loop from index 3 (4th element) to the length of the array
   a. see which of the already found numbers is bigger than the current element (if any)
   b.replace the biggest of the found numbers with the new number if at least one was found
4. print out or return the numbers you found
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文