这个问题可能已经回答了,但是我的问题是,如果我在给定的数组中传递重复或重复的值,则输出错误。按照逻辑,应该给出正确的值,但不能确定我的代码出了什么问题。下面是我的代码
import java.util.Scanner;
import java.util.Arrays;
class ThirdLargest{
static int thirdLar(int arr[],int arr_size)
{
int i, largest, secondLargest, thirdLargest;
thirdLargest = largest = secondLargest = Integer.MIN_VALUE;
for (i = 0; i < arr_size ; i ++)
{
/* If current element is smaller than
largest*/
if (arr[i] > largest)
{
thirdLargest = secondLargest;
secondLargest = largest;
largest = arr[i];
}
/* If arr[i] is in between largest and
secondLargest then update secondLargest */
else if (arr[i] > secondLargest)
{
thirdLargest = secondLargest;
secondLargest = arr[i];
}
else if (arr[i] > thirdLargest)
thirdLargest = arr[i];
}
return thirdLargest;
}
public static void main(String args[]){
//Scanner object for capturing the user input
Scanner scanner = new Scanner(System.in);
//Stored the entered value in variable
/*System.out.print("Enter 5 number of elements:");
int n = scanner.nextInt();*/
int n = 5;
int arr[] = new int[n];
System.out.println("Enter 5 numbers one by one:");
for(int i = 0; i < n; i++)
{
arr[i] = scanner.nextInt();
}
//Call thirdLar method to find largest number among given numbers
int thirdLarNum = thirdLar(arr,n);
System.out.println("The Third Largest Number is: "+thirdLarNum);
}
}
输出:如果我传递的值如10 20 40 20 20 正确的输出应为10,但其返回值为20
答案 0 :(得分:5)
要确保largest
,secondLargest
和thirdLargest
是唯一的,您应该添加更多支票:
...
else if (arr[i] > secondLargest && arr[i] != largest)
--------------------
{
thirdLargest = secondLargest;
secondLargest = arr[i];
}
else if (arr[i] > thirdLargest && arr[i] != largest && arr[i] != secondLargest)
-----------------------------------------------
thirdLargest = arr[i];
...
答案 1 :(得分:3)
您可以使用Set
而不是数组来消除重复的值并将其转换为数组
Set<int> set = new HashSet<>();
for(int i = 0; i < n; i++)
{
set.add(scanner.nextInt());
}
int thirdLarNum = thirdLar(set.toArray(), n);
或者在set
中使用thirdLar
而不是数组
static int thirdLar(Set<int> set)
{
for (int number : set) {
if (number > largest) {
//...
}
}
}
答案 2 :(得分:2)
您可以使用Streams:
我在这里使用的IntStream不支持排序的Comparator,这可能允许Collections.reverseSort(),因此在比较之前,我必须对每个int求反,然后返回求反的值;
import java.util.Arrays;
public class Main {
static int thirdLar(final int[] arr) {
final int[] array = Arrays.stream(arr).map(i -> -i).sorted().distinct().limit(3).toArray();
return -array[Math.min(2, array.length - 1)];
}
public static void main(final String args[]) {
System.out.println(thirdLar(new int[] { 1, 5, 2, 5, 10, 2, 2, 5, 10, 12, 25, 1, 5 }));
}
}
编辑:此代码可轻松找到第n大
import java.util.Arrays;
public class Main {
static int nthLargest(final int[] arr, final int rank) {
assert rank > 0;
final int[] array = Arrays.stream(arr).map(i -> -i).sorted().distinct().limit(rank).toArray();
return -array[Math.min(rank - 1, array.length - 1)];
}
public static void main(final String args[]) {
System.out.println(nthLargest(new int[] { 1, 5, 2, 5, 10, 2, 2, 5, 10, 12, 25, 1, 5 }), 3);
}
}
答案 3 :(得分:0)
我们可以使用以下代码来实现这一目标。
@timestamp, _id, _index, _score, _type, log, stream