在Java中找到第三大的no

时间:2012-09-05 10:44:14

标签: java arrays

我对这个简单的程序感到困惑。我必须在数组中找到第三大的no。我已经完成了一些代码,但在第三大代码中只获得了第二大没有问题,请告诉我这个解决方案有什么问题:

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);
    }
}

11 个答案:

答案 0 :(得分:12)

我会尝试这样的事情:

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];
}

未经测试,但我认为不再需要第二个IF。

代码说明:

我们正在验证如果输入的数字大于最大值,则将第三个,第二个和第一个最大值向上移动一级。如果输入的值大于第二大且小于最大值,则将3和2向上移动一级。如果输入的值大于第三大且小于第二大,则将第三大值移至输入值。

答案 1 :(得分:5)

Collections 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));

答案 2 :(得分:2)

        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];
        }

答案 3 :(得分:1)

使用java列表对其进行排序。拿第三个元素。

java.util.Collections.sort()

答案 4 :(得分:1)

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];
        }

 }

答案 5 :(得分:1)

使用Integer数组,然后使用Collections对其进行排序,只需选择所需的元素:

代码:

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);

输出结果为:

[0, 1, 2, 3, 6]

所以,现在选择第3个大号为list.get(list.size()-3))

您也可以对Collection进行反向排序。检查它是documentation

答案 6 :(得分:1)

如果您希望该代码有效,我认为问题在于:

 if (secondlargest < arr[i] && largest != arr[i]) {
                thirdlargest = secondlargest;
                secondlargest = arr[i];
                if (thirdlargest < arr[i] && secondlargest != arr[i])
                    thirdlargest = arr[i];
            }

问题是你将thirdLargest设置为secondLargest,已经被识别为小于arr [i]。然后,您正在测试thirdLargest是否小于arr [i](保证它在外部条件下设置为第二大),然后将其设置为arr [i]。尝试删除

if (thirdlargest < arr[i] && secondlargest != arr[i])
                        thirdlargest = arr[i];

如果这不起作用,请尝试添加第三个单独的条件,以涵盖arr [i]小于secondGreatest但大于thirdGreatest的情况。 (见上面的Jens回答),类似于:

答案 7 :(得分:0)

只需遍历整个阵列并跟踪三个最大的数字。

或者您可以对其进行排序,然后从顶部返回第三个元素。

答案 8 :(得分:0)

一旦读入数组,只需在数组上调用Arrays.sort(数组)(在你的例子中是Arrays.sort(arr);)。这将按升序排序,然后您可以检索元素arr.length-2

此处完整描述: http://www.homeandlearn.co.uk/java/sorting_arrays.html

示例:

    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();
        }
    Arrays.sort(arr);
System.out.println("Second Largest number is: " + arr[4]
                + "\nThird largest number is=====" + arr[3]);

答案 9 :(得分:0)

试试这段代码,

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]);
}   

答案 10 :(得分:0)

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);
    }

}