2

大家好,我想找出数组中第二大的不接受负数。我使用了下面的代码,这个显示第二大没有只有正面没有。所以请建议我如何做到这一点。

class ArrayExample {
    public static void main(String[] args) {
        int secondlargest = 0;
        int largest = 0;
        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])
                secondlargest = arr[i];
        }
        System.out.println("Second Largest number is: " + secondlargest);
    }
}
4

9 回答 9

7

问题来自您在以下行中将两个变量初始化为 0 的事实:

int secondlargest = 0;
int largest = 0;

相反,您应该将它们初始化为Integer.MIN_VALUE,然后它也适用于负值。

于 2012-05-23T08:31:13.387 回答
0

将最大和次大初始化为Integer.MIN_VALUE而不是零。

于 2012-05-23T08:31:45.390 回答
0

secondlargest用最小的largest负值 初始化变量。
使用此代码:

class ArrayExample {
        public static void main(String[] args) {
            int secondlargest = 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])
                    secondlargest = arr[i];
            }
            System.out.println("Second Largest number is: " + secondlargest);
        }
    }
于 2012-05-23T08:36:36.627 回答
0

这对我来说适用于正值和负值。在循环之前,您需要用非常小的值初始化largestandsecondlargest变量,通过这样做,您可以(几乎)确保数组中的所有其他值都大于它们:

int largest = Integer.MIN_VALUE;
int secondlargest = Integer.MIN_VALUE;

循环内部:

if (arr[i] > largest) {
    secondlargest = largest;
    largest = arr[i];
}

else if (arr[i] != largest && arr[i] > secondlargest) {
    secondlargest = arr[i];
}

循环后:

if (secondlargest != Integer.MIN_VALUE)
    System.out.println("Second Largest number is: " + secondlargest);

请注意,对于数组中的所有元素恰好是 (不太可能)的情况,需要最后一次检查Integer.MIN_VALUE

于 2012-05-23T08:37:43.327 回答
0

我认为这种方法可能很有用,它需要 n+log(n)-2 次比较

 import java.util.ArrayList;


 public class SecondLargest {
 static ArrayList<ArrayList<Integer>> level = new ArrayList<ArrayList<Integer>>();

public static void main(String[] args) {
    int input[]={9,8,7,4,5,6,1,2,3,1,1,21,33,32,1,2,3,12,3,2,1};
    ArrayList<Integer> arr= new ArrayList<Integer>();

    for(int i=0;i<input.length;i++){
        arr.add(input[i]);
    }
    level.add(arr);
    seconLarger(arr);
    System.out.println(SecondLarge(level));
}

private static ArrayList<Integer> seconLarger(ArrayList<Integer> arr) {
    ArrayList<Integer> tmp= new ArrayList<Integer>();
    if (arr.size()==1)
    {
        return arr;
    }

if(arr.size()%2==0)
    {
        for(int i=0;i<arr.size();i=i+2)
        {
            if(arr.get(i)>arr.get(i+1))
            {
                tmp.add(arr.get(i));
            }
            else
            {
                tmp.add(arr.get(i+1));
            }
        }


    }   
else 
    {
        for(int i=0;i<arr.size()-1;i=i+2)
        {
            if(arr.get(i)>arr.get(i+1))
            {
                tmp.add(arr.get(i));
            }
            else
            {
                tmp.add(arr.get(i+1));
            }
        }
        tmp.add(arr.get(arr.size()-1));
    }
level.add(tmp);
return seconLarger(tmp);
}

private static int SecondLarge(ArrayList<ArrayList<Integer>> li)
{
    int li_size=li.size();
    int large=li.get(li_size-1).get(0);
    int secondlarge=0;
    int tmp=0;
    for(int i=li_size-2;i>=0;i--)
    {
        ArrayList<Integer> arr = li.get(i);
        if(large==arr.get(tmp))
        {
            if(tmp+1<arr.size())
            {
                if(secondlarge<arr.get(tmp+1))
                {
                    secondlarge=arr.get(tmp+1);
                }
            }
        }
        else
        {
            if(secondlarge<arr.get(tmp))
            {
                secondlarge=arr.get(tmp);
            }
            tmp=tmp+1;

        }

        tmp=tmp*2;
    }
    return secondlarge;
}}
于 2013-02-11T09:16:35.127 回答
0
package com.demo.mum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author cyruses
 * 
 */
public class SecondLargest {
    public static int largest = Integer.MIN_VALUE;
    public static int secondLargest = Integer.MIN_VALUE;

    public static void main(String args[]) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Size of Array:");
        int n = Integer.parseInt(br.readLine());
        int a[] = new int[n];
        System.out.println("Enter the elements on array:");
        for (int i = 0; i < a.length; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        System.out.println("Elements you entered are:");
        for (int i = 0; i < a.length; i++) {
            System.out.println("a[" + i + "]" + "=" + a[i]);
        }
        if (a.length <= 2) {
            if (a[0] == a[1]) {
                System.out.println("Theres no second largest number in your array");
            } else {
                System.out.println("SecondLargest:" + secondLargest(a));
            }
        } else {
            System.out.println("SecondLargest:" + secondLargest(a));
        }
    }

    private static int secondLargest(int[] a) {

        for (int i = 0; i < a.length; i++) {
                if (a[i] > largest) {
                    secondLargest = largest;
                    largest = a[i];
                } else if (a[i] > secondLargest) {
                    secondLargest = a[i];
                }
            }
        return secondLargest;
    }
}
于 2014-04-28T09:36:18.530 回答
0

或者这里是一个不依赖 Integer.MIN_VALUE 的花哨的 Java 实现

int findSecondHighest(int[] arr){
    if(arr == null || arr.length < 2){
        throw new IllegalArgumentException();
    }

    int fh,sh;

    if(arr[0]>=arr[1]){
        fh = arr[0];
        sh = arr[1];
    }else{
        fh = arr[1];
        sh = arr[0];
    }

    for (int i = 2; i < arr.length; i++) {
        if(fh < arr[i]){
            sh = fh;
            fh = arr[i];
        }else if(sh < arr[i]){
            sh = arr[i];
        }
    }

    return sh;
}
于 2016-04-03T13:23:03.990 回答
0

而不是将第二大数字初始化为最小值或零。您可以按照以下代码获得第二大数字。

import java.util.Scanner;

public class SecondLargestNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a;
        int n;
        System.out.println("Enter number of elements");
        n = scanner.nextInt();
        a = new int[n];
        for(int i=0; i<n; i++) {
            a[i] = scanner.nextInt();
        }
        int secondLargestNumber = a[0];
        int largestNumber = a[0];
        int count = 1;
        for(int i=1; i<a.length; i++) {
            if(a[i] >= largestNumber) {
                if(a[i] == largestNumber) {
                    count++;
                } else {
                    count = 1;
                }
                secondLargestNumber = largestNumber;
                largestNumber = a[i];
            } else {
                if(secondLargestNumber == largestNumber && count == 1) {
                    secondLargestNumber = a[i];
                } else if(a[i] > secondLargestNumber) {
                    secondLargestNumber = a[i];
                }
            }
        }
        System.out.println("Second Largest Number: " + secondLargestNumber);
    }
}
于 2016-09-30T13:01:52.930 回答
0
public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.println("Enter array size = ");
    int size=in.nextInt();
    int[] n = new int[size];
    System.out.println("Enter "+ size +" values ");

    for(int i=0;i<n.length;i++)
        n[i] = in.nextInt();
    int big=n[0],sbig=n[0];

    // finding big, second big
    for(int i=0;i<n.length;i++){
        if(big<n[i]){
            sbig=big;
            big=n[i];
            }else if(sbig<n[i])
                sbig=n[i];
    }
    // finding second big if first element itself big
    if(big==n[0]){
        sbig=n[1];
        for(int i=1;i<n.length;i++){
            if(sbig<n[i]){
                sbig=n[i];
                }
        }
    }       

    System.out.println("Big "+ big+" sBig "+ sbig);

    in.close();
}
于 2017-01-28T18:32:24.527 回答