在 Java 中查找第三大数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12279667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find third largest no in Java
提问by Dilip
I'm little confused with this simple program.I have to find third largest no in array.I have done some code but getting only second largest no problem in third largest no so please suggest me what is wrong with this solution:
我对这个简单的程序有点困惑。我必须在数组中找到第三大没有。我已经做了一些代码,但在第三大没有得到第二大没有问题所以请告诉我这个解决方案有什么问题:
class ArrayExample {
public static void main(String[] args) {
int secondlargest = Integer.MIN_VALUE;
int thirdlargest = Integer.MIN_VALUE;
int largest = Integer.MIN_VALUE;
Scanner input = new Scanner(System.in);
System.out.println("Enter array values: ");
int arr[] = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();
if (largest < arr[i]) {
secondlargest = largest;
largest = arr[i];
}
if (secondlargest < arr[i] && largest != arr[i]) {
thirdlargest = secondlargest;
secondlargest = arr[i];
if (thirdlargest < arr[i] && secondlargest != arr[i])
thirdlargest = arr[i];
}
}
System.out.println("Second Largest number is: " + secondlargest
+ "\nThird largest number is=====" + thirdlargest);
}
}
回答by Jens
I would try something like this:
我会尝试这样的事情:
if (largest < ar[i]) {
thirdlargest = secondlargest;
secondlargest = largest;
largest = arr[i];
} else if (secondlargest < ar[i]) {
thirdlargest = secondlargest;
secondlargest = ar[i];
} else if (thirdlargest < ar[i]) {
thirdlargest = ar[i];
}
Not tested but I think the second IF isn't needed anymore.
未经测试,但我认为不再需要第二个 IF。
Code Explanation:
代码说明:
We are verifying that if an entered number is greater than largest then move the third, second and 1st largest values one level up. If an entered value is greater than 2nd largest and less than largest, then move 3 and 2 one level up. If entered values is greated than 3rd largest and less than 2nd largest then move 3rd largest to the entered value.
我们正在验证如果输入的数字大于最大值,则将第三、第二和第一最大值向上移动一级。如果输入的值大于第二大值且小于最大值,则将 3 和 2 向上移动一级。如果输入的值大于第三大值且小于第二大值,则将第三大值移至输入值。
回答by Nos
Collections API. Here is an example:
集合 API。下面是一个例子:
List list = Arrays.asList(new Integer[] {1, 2, 29, 4, 28, 6, 27, 8});
Collections.sort(list);
System.out.print(list.get(list.size()-3));
回答by Sujith Mohan
if(firstLargest<array[num])
{
thirdLargest=secondLargest;
secondLargest=firstLargest;
firstLargest = array[num];
}
else if((secondLargest<array[num])&&(array[num]!=firstLargest))
{
thirdLargest=secondLargest;
secondLargest = array[num];
}
else if((thirdLargest<array[num])&&(array[num]!=secondLargest))
{
thirdLargest = array[num];
}
回答by perilbrain
for (int i = 0; i < arr.length; i++)
{
arr[i] = input.nextInt();
if (largest < arr[i]) {
secondlargest = largest;
largest = arr[i];
continue;
}
if (secondlargest <= arr[i] && largest > arr[i])
{
thirdlargest = secondlargest;
secondlargest = arr[i];
continue;
}
if (thirdlargest <= arr[i] && secondlargest > arr[i])
{
thirdlargest = arr[i];
}
}
回答by mtk
Use Integer
array and then sort it using Collections
and just pick the element you need:
使用Integer
数组,然后使用它进行排序,然后Collections
选择您需要的元素:
Code:
代码:
System.out.println("Enter array values: ");
Integer arr[] = new Integer[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();
}
List<Integer> list = Arrays.asList(arr);
Collections.sort(list);
System.out.println(list);
The output is:
输出是:
[0, 1, 2, 3, 6]
So, now select the 3rd larget number as list.get(list.size()-3))
.
所以,现在选择第三大数作为list.get(list.size()-3))
。
You can also reverse sort the Collection. Check it's documentation.
您还可以对集合进行反向排序。检查它的文档。
回答by Joseph Gardiner
If you want that code to work, I think the problem is here:
如果您希望该代码起作用,我认为问题就在这里:
if (secondlargest < arr[i] && largest != arr[i]) {
thirdlargest = secondlargest;
secondlargest = arr[i];
if (thirdlargest < arr[i] && secondlargest != arr[i])
thirdlargest = arr[i];
}
The issue is you are setting thirdLargest to be secondLargest, which has already been identified as less than arr[i]. You are then testing if thirdLargest is less than arr[i] (which it is guaranteed to be as it it has been set to second largest within the outer condition) and then setting it to arr[i]. Try removing the
问题是您将thirdLargest 设置为secondLargest,它已被确定为小于arr[i]。然后,您将测试第三个最大是否小于 arr[i](它保证是因为它已在外部条件中设置为第二大),然后将其设置为 arr[i]。尝试删除
if (thirdlargest < arr[i] && secondlargest != arr[i])
thirdlargest = arr[i];
and if that doesn't work try adding a third separate condition to cover cases where arr[i] is less than secondGreatest but greater then thirdGreatest. (see Jens answer above), something like :
如果这不起作用,请尝试添加第三个单独的条件来涵盖 arr[i] 小于 secondGreatest 但大于 thirdGreatest 的情况。(请参阅上面的 Jens 回答),例如:
回答by NimChimpsky
Use a java list, sort it. Take the third element.
使用 java 列表,对其进行排序。取第三个元素。
回答by unknown
Try this code,
试试这个代码,
public static void main(String[] args) {
int arr[] = {67, 56, 87, 42};
for (int i = 0; i <arr.length - 1; i++) {
if (arr[i] < arr[i + 1]) {
int swap = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = swap;
}
}
System.out.println("third highest element is: " + arr[2]);
}
回答by Narasingha Pattnayak
package algo;
public class LargestNumbers {
public static void main(String args[]){
int arr[] = new int[]{5,2,3,4,6};
int largest=Integer.MIN_VALUE;;
int secondLargest=Integer.MIN_VALUE;;
int thirdLargest=Integer.MIN_VALUE;;
for(int i=0;i<arr.length;i++){
if(largest<arr[i])
{
thirdLargest=secondLargest;
secondLargest=largest;
largest = arr[i];
}
else if((secondLargest<arr[i])&&(arr[i]!=largest))
{
thirdLargest=secondLargest;
secondLargest = arr[i];
}
else if((thirdLargest<arr[i])&&(arr[i]!=secondLargest))
{
thirdLargest = arr[i];
}
}//for
System.out.println("Numbers are: " + largest + " " + secondLargest
+ "\nThird largest number is=====" + thirdLargest);
}
}
回答by Infiltrator
Just cycle through the whole array and keep track of the three largest numbers.
只需循环遍历整个数组并跟踪三个最大的数字。
Or you could sort it and then return the third element from the top.
或者您可以对它进行排序,然后从顶部返回第三个元素。