简体   繁体   English

想要找到最高最大数和第二大最大数及其索引值

[英]Want to find Highest Maximum number and Second Highest Maximum Number with their Index value

This code finds the highest number and second highest number but gives the wrong index number, but only in some cases When array values: {1,3,3,0,3} When Array Values: {3,3,0,1,2}这段代码找到了最高数和次高数但给出了错误的索引号,但仅在某些情况下 当数组值:{1,3,3,0,3} 当数组值:{3,3,0,1, 2}

If all are unique numbers then it gives an accurate answer with accurate index value;如果都是唯一的数字,那么它会给出准确的索引值的准确答案; Where do I need to change the code to get accurate index value for above cases?对于上述情况,我需要在哪里更改代码才能获得准确的索引值?

FirstMaxNumber=arrFindIndex[0];
SecondMaxNumber=arrFindIndex[0];
FirstMaxRatingIndex=0;
SecondMaxRatingIndex=0;

for (int i = 0; i < arrSize; i++)
{
     if (FirstMaxNumber <= arrFindIndex[i])
     {
         SecondMaxNumber = FirstMaxNumber;
         FirstMaxNumber = arrFindIndex[i];
         FirstMaxRatingIndex = i;
      }
      else if (SecondMaxNumber <= arrFindIndex[i])
      {
         SecondMaxNumber = arrFindIndex[i];
         SecondMaxRatingIndex = i;
      }
}

// print(FirstMaxNumber);
// Print(FirstMaxRatingIndex);

// print(SecondMaxNumber);
// print(SecondMaxRatingIndex);

In the first if statement, the value of the second maximum value is set:在第一个 if 语句中,设置了第二个最大值的值:

SecondMaxNumber = FirstMaxNumber;

but the index isn't:但索引不是:

SecondMaxRatingIndex = FirstMaxRatingIndex;
FirstMaxRatingIndex = i;

Something like this:像这样的东西:

FirstMaxNumber = arrFindIndex[0];
SecondMaxNumber = arrFindIndex[0];

FirstMaxRatingIndex = 0;
SecondMaxRatingIndex = 0;

// Do not use magic values: "arrSize" but actual length: arrFindIndex.Length
for (int i = 1; i < arrFindIndex.Length; i++) {
  int v = arrFindIndex[i];

  if (v > FirstMaxNumber) {
    // so "first" becomes "second"
    SecondMaxNumber = FirstMaxNumber;
    SecondMaxRatingIndex = FirstMaxRatingIndex;

    FirstMaxNumber = v;
    FirstMaxRatingIndex = i;
  }
  else if ((v > SecondMaxNumber) || (i == 1)) {
    SecondMaxNumber = v;
    SecondMaxRatingIndex = i;
  }
}

Why don't you just use LINQ for that?为什么不直接使用 LINQ 呢?

var arr = new [] {3, 0, 4, 2, 3, 7};
var min = arr.Select((Val, Key) => new { Val, Key }).First(x => x.Val == arr.Min());
var max = arr.Select((Val, Key) => new { Val, Key }).First(x => x.Val == arr.Max());

Console.WriteLine($"Min Key: {min.Key} Val: {min.Val} \nMax Key: {max.Key} Val: {max.Val}");

// Output
// Min Key: 1 Val: 0 
// Max Key: 5 Val: 7

If the arrays are short and you don't particularly care how fast it is, the easiest code is something like this:如果数组很短并且您并不特别关心它的速度,那么最简单的代码是这样的:

var indices = Enumerable.Range(0, array.Length).ToArray();
Array.Sort(array, indices, Comparer<int>.Create((a, b) => b - a));

Then indices will contain the indices of all the elements, in descending order, so you just need the first two.然后indices将包含所有元素的索引,按降序排列,所以你只需要前两个。

Here's a compilable console app (requires .Net 4.5 or later):这是一个可编译的控制台应用程序(需要 .Net 4.5 或更高版本):

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    static class Program
    {
        static void Main()
        {
            test(1, 3, 3, 0, 3); // Prints 1, 2
            test(3, 3, 0, 1, 2); // Prints 0, 1
        }

        static void test(params int[] array)
        {
            var indices = Enumerable.Range(0, array.Length).ToArray();
            Array.Sort(array, indices, Comparer<int>.Create((a,b)=>b-a));
            Console.WriteLine($"{indices[0]}, {indices[1]}");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM