0

我必须找到第一个、第二个和第三个最大的数组。我知道我可以简单地对其进行排序并返回数组 [0]、数组 [1]、数组 [3]。但问题是,我需要索引,而不是值。例如,如果我有float[] listx={8.0, 3.0, 4.0, 5.0, 9.0}它应该返回 4、0 和 3。

这是我的代码,但它不起作用:

//declaration max1-3        
public void maxar (float[] listx){

    float maxel1=0;
    float maxel2=0;
    float maxel3=0;

    for (int i=0; i<listx.length; i++){
        if(maxel1<listx[i])
        {maxel1=listx[i];
        max1=i;
        }
    }
    listx[max1]=0; //to exclude this one in nextsearch

    for (int j=0; j<listx.length; j++){
        if(listx[j]>maxel2)
        {maxel2=listx[j];
        max2=j;
        }
    }
    listx[max2]=0;

    for (int k=0; k<listx.length; k++){
        if(listx[k]>maxel3)
        {maxel3=listx[k];
        max3=k;
        }
    }
}

我得到 max1 正确,但之后所有元素都变为 0。因此 max2 和 max3 变为 0。请告诉我这个解决方案有什么问题。谢谢你。

4

7 回答 7

2

您可以使用单个循环找到三个元素,并且不需要修改数组。

当您遇到一个新的最大元素时,您需要将之前的最大元素和之前的第二大元素向下移动一个位置。

同样,当你找到一个新的第二大元素时,你需要转移maxel2maxel3.

您可能希望使用一个数组,而不是使用这三个变量。这将使您能够简化逻辑,并使其易于推广到k最大的元素。

于 2012-12-01T18:50:58.480 回答
0

对数组进行 3 次遍历:在第一遍查找值和最大元素的第一个索引M1,在第二遍查找值和最大元素的第一个索引M2小于M1和第三遍查找值/第一个索引M3 < M2

于 2012-12-01T18:42:53.270 回答
0

试试这个代码它会工作:)

  public class Array
    {
  public void getMax( double ar[] )
   {
    double max1 = ar[0]; // Assume the first
    double max2 = ar[0]; // element in the array
    double max3 = ar[0]; // is the maximum element.
    int ZERO = 0; 
     // Variable to store inside it the index of the max value to set it to zero.

    for( int i = 0; i < ar.length; i++ )
    {
        if( ar[i] >= max1)
        {
            max1 = ar[i];
            ZERO = i;
        }
    }

    ar[ZERO] = 0; // Set the index contains the 1st max to ZERO.

    for( int j = 0; j < ar.length; j++ )
    {
        if( ar[j] >= max2 )
        {
            max2 = ar[j];
            ZERO = j;
        }
    }

    ar[ZERO] = 0; // Set the index contains the 2st max to ZERO.

    for( int k = 0; k < ar.length; k++ )
    {
        if( ar[k] >= max3 )
        {
            max3 = ar[k];
            ZERO = k;
        }
    }

            System.out.println("1st max:" + max1 + ", 2nd: " +max2 + ",3rd: "+ max3);                              
   }

public static void main(String[] args)
{
    // Creating an object from the class Array to be able to use its methods.
    Array array = new Array();
    // Creating an array of type double.
    double a[] = {2.2, 3.4, 5.5, 5.5, 6.6, 5.6};

    array.getMax( a ); // Calling the method that'll find the 1st max, 2nd max, and      and 3rd max.
}

  }
于 2012-12-01T18:53:15.397 回答
0
public class ArrayExample {
    public static void main(String[] args) {
        int secondlargest = 0;
        int thirdLargest=0;
        int largest = 0;
        int arr[] = {5,4,3,8,12,95,14,376,37,2,73};
        for (int i = 0; i < arr.length; i++) {
            if (largest < arr[i]) {
                secondlargest = largest;
                largest = arr[i];
            }
            if (secondlargest < arr[i] && largest != arr[i])
                secondlargest = arr[i];
            if(thirdLargest<arr[i] && secondlargest!=arr[i] && largest!=arr[i] && thirdLargest<largest && thirdLargest<secondlargest)
                thirdLargest =arr[i];
        }
        System.out.println("Largest number is: " + largest);
        System.out.println("Second Largest number is: " + secondlargest);
        System.out.println("third Largest number is: " + thirdLargest);
    }
}
于 2014-08-27T05:28:14.383 回答
0

我建议进行一次优化而不是三次。下面的代码对我有用。请注意,代码没有断言listx至少有 3 个元素。如果它仅包含 2 个或更少元素,则由您决定应该发生什么。

我喜欢这段代码的地方在于它只对数组进行一次传递,在最好的情况下,与进行三次传递相比,它的运行时间更快,其因子与listx.

假设i1i2i3将三个最大元素的索引存储在 中listx,并且i0是指向最小元素的i1、i2i3之一。一开始,i1 = i2 = i3因为我们还没有找到最大的元素。所以让i0 = i1。如果我们找到一个新的索引j使得listx[j] > listx[i0],我们设置i0 = j,用一个导致更大元素的索引替换旧索引。然后我们找到i1、i2i3中的索引,它现在导致三个元素中最小的元素,这样我们就可以安全地丢弃以防出现新的大元素。

注意:这段代码是 C 语言的,所以如果你想使用它,请把它翻译成 Java。我确保使用类似的语法来简化它。(我是用 C 写的,因为我缺少 Java 测试环境。)

void maxar(float listx[], int count) {
    int maxidx[3] = {0};

    /* The index of the 3rd greatest element
     * in listx.
     */
    int max_3rd = 0;

    for (int i = 0; i < count; i++) {
        if (listx[maxidx[max_3rd]] < listx[i]) {
            /* Exchange 3rd greatest element
             * with new greater element.
             */
            maxidx[max_3rd] = i;

            /* Find index of smallest maximum. */
            for (int j = (max_3rd + 1) % 3; j != max_3rd; j = (j + 1) % 3) {
                if (listx[maxidx[j]] < listx[maxidx[max_3rd]]) {
                    max_3rd = j;
                }
            }
        }
    }

    /* `maxidx' now contains the indices of
     * the 3 greatest values in `listx'.
     */

    printf("3 maximum elements (unordered):\n");
    for (int i = 0; i < 3; i++) {
        printf("index: %2d, element: %f\n", maxidx[i], listx[maxidx[i]]);
    }
}
于 2012-12-01T21:10:25.877 回答
0
def third_mar_array(arr):
    max1=0
    max2=0
    max3=0
    for i in range(0,len(arr)-1):
         if max1<arr[i]:
             max1=arr[i]
             max_in1=i
    arr[max_in1]=0
    for j in range(0,len(arr)-1):
         if max2<arr[j]:
             max2=arr[j]
             max_in2=j
    arr[max_in2]=0
    for k in range(0,len(arr)-1):
         if max3<arr[k]:
             max3=arr[k]
             max_in3=k
    #arr[max_in3]=0
    return max3

n=[5,6,7,3,2,1]

f=first_array(n)
print f
于 2015-12-05T16:55:56.467 回答
0
import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testcase = sc.nextInt();

        while (testcase-- > 0) {
            int sizeOfArray = sc.nextInt();
            int[] arr = new int[sizeOfArray];
            for (int i = 0; i < sizeOfArray; i++) {
                arr[i] = sc.nextInt();
            }

            int max1, max2, max3;
            max1 = 0;
            max2 = 0;
            max3 = 0;

            for (int i = 0; i < sizeOfArray; i++) {
                if (arr[i] > max1) {
                    max3 = max2;
                    max2 = max1;
                    max1 = arr[i];
                }
                else if (arr[i] > max2) {
                    max3 = max2;
                    max2 = arr[i];
                }
                else if (arr[i] > max3) {
                    max3 = arr[i];
                }
            }

            System.out.println(max1 + " " + max2 + " " + max3);
        }
    }
}
于 2021-06-30T12:18:36.087 回答