如何找到阵列中最大,第二大和第三大的数字,然后显示它们的序列位置?

时间:2013-09-09 19:24:36

标签: java arrays

到目前为止,我有这个:

public static void highV()
{
    KeyboardReader reader = new KeyboardReader();

    int numVal = 0;

    while (numVal < 3) // Makes sure 3 or more numbers are entered
    {
        numVal = reader.readInt("How many values would you like to enter (3 or more): ");

        if (numVal < 3)
        {
            System.out.println("Invalid Entry");
        }
    }

    int[] dval = new int[numVal];

    int i;
    int j;
    int k;
    int a;
    int high = 0;
    int sec = 0;
    int thr = 0;

    System.out.println();
    for (i = 0; i < dval.length; i++) // Reads in numbers and stores them in an array
    {
        dval[i] = reader.readInt("Enter value number " + (i + 1) + ". ");

    }



    System.out.println();
    System.out.print("List of values: "); 
    for (j = 0; j < dval.length; j++)// Prints out a list of values
    {
        if (j == (dval.length)-1)
        {
            System.out.println(dval[j]);
        }
        else
        {
            System.out.print(dval[j] + ", ");
        }

    }
    System.out.println();

    System.out.println("There was a total of " + dval.length + " numbers entered.");

    System.out.println();

    for (k = 0; k < dval.length; k++) // Determines the highest second highest and third highest numbers
    {
        if (dval[k] > high)
        {
            int oldSec = sec;
            sec = high;
            thr = oldSec;
            high = dval[k];
        }
        else if (dval[k] > sec)
        {
            thr = sec;
            sec = dval[k];
        }
        else if (dval[k] > thr)
        {
            thr = dval[k];
        }
    }


    for (a = 0; a < dval.length; a++) // Determines sequence location of first second and third highest numbers
    {
        if (dval[a] == high)
        {
            high = a+1;
        }
        if (dval[a] == sec)
        {
            sec = a+1;
        }
        if (dval[a] == thr)
        {
            thr = a+1;
        }
    }

    System.out.println("The highest number was in sequence #: " + high);
    System.out.println("The second highest number was in sequence #: " + sec);
    System.out.println("The third highest number was in sequence #: " + thr);
    System.out.println();
}

这几乎适用于所有内容,除非输入的数字都是降序。示例:如果输入5,4,3,2,1,当得到1,2,3时,得到5,4,3作为答案。

如果输入2,18,5,3,1,0,9,100,则会得到8,2,7

的正确答案

有什么想法吗?

6 个答案:

答案 0 :(得分:1)

    if (dval[a] == high)
    {
        high = a+1;
    }
    if (dval[a] == sec)
    {
        sec = a+1;
    }
    if (dval[a] == thr)
    {
        thr = a+1;
    }

当您确定它们的索引时,您将重复使用相同的变量。在5,4,3,2,1的情况下,高首先设置为1,这将在以后匹配。

引入3个新变量highIndsecIndthrInd,这应该可以解决您的问题。

在for循环之上:

int highInd=0;
int secInd=0;
int thrInd=0;

在for循环中:

if (dval[a] == high)
{
    highInd = a+1;
}
if (dval[a] == sec)
{
    secInd = a+1;
}
if (dval[a] == thr)
{
    thrInd = a+1;
}

试试这个。当您进行打印时,请将变量名称更改为这些。

答案 1 :(得分:1)

此块可能存在问题,因为您将highsecthr重新用于表示数组的值以表示数组的索引。

不仅如此,您依赖于highsecthr,它们是整个循环中数组的值。

for (a = 0; a < dval.length; a++) // Determines sequence location of first second and third highest numbers
{
    if (dval[a] == high)
    {
        high = a+1;
    }
    if (dval[a] == sec)
    {
        sec = a+1;
    }
    if (dval[a] == thr)
    {
        thr = a+1;
    }
}

在第一次迭代后,high将为5,(正确),但您会将其设置为1,并在输出中显示。

但是当你完成第二次迭代,high1,而a1时,条件:(dval[a] == high)将为真,但是错误的,类似的错误会在整个循环中发生。

我会强烈建议使用不同的变量来跟踪您的值的索引,而不是用于跟踪您的值的索引。

答案 2 :(得分:0)

我有点想写这个,但为什么不立刻处理所有的处理。一次性获取索引值和实际值。如果找到一个更大的值并继续通过for循环运输,则将值向下滚动。

此代码未进行全面测试,但更多的示例是在您找到更大的值时显示滚动值。

int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
int thirdLargest = Integer.MIN_VALUE;

int firstLargestIndex = -1;
int secondLargestIndex = -1;
int thirdLargestIndex = -1;

// loop through array, check for a higher value than values 
// that have already been saved, if necessary, roll the values down
// and save the current value
for (j = 0; j < dval.length; j++) { 
    if(dval[j] > firstLargest) {
        thirdLargestIndex = secondLargestIndex;
        secondLargestIndex = firstLargestIndex;
        firstLargestIndex = j;

        thirdLargest = secondLargest;
        secondLargest = firstLargest;
        firstLargest = dval[j];
    } else if(dval[j] > secondLargest) {
        thirdLargestIndex = secondLargestIndex;
        secondLargestIndex = j;

        thirdLargest = secondLargest;
        secondLargest = dval[j];
    } else if(dval[j] > thirdLargest) {
        thirdLargestIndex = j;

        thirdLargest - dval[j];
    }
}

答案 3 :(得分:0)

如果您对替代方法感兴趣,可以创建Map将数字映射到其索引。 E.g。

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numList.Length; i++)
    map.put(numList[i], i);
List<Integer> sortedList = // get descending list of keys from map
for (int i = 0; i < 3; i++)
    System.out.println(String.valueOf(numList.get(i) + 1));

我更喜欢这个解决方案,因为它更短,因此,恕我直言,更易读,更容易调试。它可能比你的慢一点,因为它有其他课程可以做一些额外的工作。

答案 4 :(得分:0)

  • 创建LinkedList
  • 排序
  • 弹出3个第一项
  • 在原始数组中找到他们的ID

    Integer data[] = new Integer[] {10,20,30,40,50,60,71,80,90,91 };
    ArrayList<Integer> originalList = new ArrayList<Integer>(Arrays.asList(data));
    LinkedList<Integer> sortedList = new LinkedList<Integer>(Arrays.asList(data));
    
    Collections.sort(sortedList,Collections.reverseOrder());
    
    Integer biggest = sortedList.pop();
    Integer second = sortedList.pop();
    Integer third = sortedList.pop();
    
    Integer indexOfBiggest = originalList.indexOf(biggest);
    Integer indexOfSecond = originalList.indexOf(second);
    Integer indexOfThird = originalList.indexOf(third);
    

答案 5 :(得分:0)

只是想法:

1-使用Integer而不是int

2-为您希望获得前三个

的排序顺序创建2个比较器

3-使用LinkedList和PriorityQueue而不是数组

坦率地说,我没有得到你的观点,但是为了获得用户输入的前三个,你可以将他输入的值存储在一个链接列表中并获得前三个

如果您有一个特定的等式来对他的企业进行排序,您可以创建一个比较器(实现比较器接口的类)并在创建优先级队列时使用它的实例(或使用此比较器对列表进行排序)和获得前三个元素

如果你能澄清你的情况,我可以提供帮助