C# 编写一个程序来查找数组中最大项的索引
在C#中编写一个控制台应用程序,以查找数组中的索引i,该索引i是数组中的最大数 如果数组中的最大元素出现多次,则需要显示最小索引 如果数组为空,则输出-1 请让我知道我的代码中有什么错误? 如果我输入数组C# 编写一个程序来查找数组中最大项的索引,c#,arrays,console-application,C#,Arrays,Console Application,在C#中编写一个控制台应用程序,以查找数组中的索引i,该索引i是数组中的最大数 如果数组中的最大元素出现多次,则需要显示最小索引 如果数组为空,则输出-1 请让我知道我的代码中有什么错误? 如果我输入数组a={1,2,46,14,64,64},例如,它返回0,而应该返回4 public static void Main() { double[] a = { 1, 9, 9, 8, 9, 2, 2 }; Console.WriteLine(MaxIndex(a));
a={1,2,46,14,64,64}代码>,例如,它返回0
,而应该返回4
public static void Main()
{
double[] a = { 1, 9, 9, 8, 9, 2, 2 };
Console.WriteLine(MaxIndex(a));
}
public static double MaxIndex(double[] array)
{
var max = double.MinValue;
int maxInd = 0, maxCount = 0;
int minIndex = 0;
var min = double.MaxValue;
for (var i = 0; i < array.Length; i++)
{
if (min > array[i])
{
min = array[i];
minIndex = i;
}
if (max == array[i])
maxCount++;
if (max < array[i])
{
maxCount = 1;
max = array[i];
maxInd = i;
}
}
if (array.Length == 0)
return -1;
if (maxCount > 1)
return minIndex;
return maxInd;
}
publicstaticvoidmain()
{
双[]a={1,9,9,8,9,2,2};
Console.WriteLine(MaxIndex(a));
}
公共静态双MaxIndex(双[]数组)
{
var max=double.MinValue;
int maxInd=0,maxCount=0;
int minIndex=0;
var min=double.MaxValue;
for(var i=0;i数组[i])
{
min=数组[i];
minIndex=i;
}
if(max==数组[i])
maxCount++;
if(最大值<数组[i])
{
最大计数=1;
max=数组[i];
maxInd=i;
}
}
如果(array.Length==0)
返回-1;
如果(最大计数>1)
返回minIndex;
返回maxInd;
}
您的代码通常过于复杂,变量太多,无法完成它的任务
您需要第一次出现的最高值的索引,因此除了MaxValueIndex和MaxValue之外,没有任何真正的理由存储任何东西
所有其他变量都应该是本地变量,以便垃圾收集可以处理它们
至于你的代码到底出了什么问题,我无能为力,因为变量名让我很困惑。
但是这里有一些代码可以实现您想要实现的目标
double[] a = { 1, 9, 9, 8, 9, 2, 2 };
var highestValueInArray = a.Max();
var arrayLength = a.Length;
for (int i = 0; i < arrayLength; i++)
{
if (a[i] == highestValueInArray)
{
Console.WriteLine(i);
break;
}
}
double[]a={1,9,9,8,9,2,2};
var highestValueInArray=a.Max();
var arrayLength=a.长度;
对于(int i=0;i
不需要手动计算最大值,只需使用yourray.max()
即可获得最大值
然后像平常一样迭代它,但是在第一次出现时,您break代码>跳出循环并返回它所在的索引
我很确定有一种方法可以将FirstOrDefault与IndexOf结合使用,但我自己无法做到这一点。试试下面这行简单的代码:
int[] numbers = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
var index = numbers.ToList().IndexOf(numbers.Max());
我认为代码足够简单,可以自我解释
但是,如果您以性能为目标,则可以省略到列表的转换,您可以编写自己的代码来查找最大数量的索引
或者更简单,由@fubo提出:
Array.IndexOf(numbers, numbers.Max());
此代码中有许多简化:
int? maxVal = null; // null because of array of negative value;
int index = -1;
for (int i = 0; i < array.Length; i++)
{
int current = array[i];
if (!maxVal.HasValue || current > maxVal.Value)
{
maxVal = current ;
index = i;
}
}
它返回零,因为minIndex实际上是零:
将minIndex更改为maxIndex:
if (maxCount > 1) return maxIndex;
要比较双精度,请使用以下代码而不是=
:
if (Math.Abs(a-b)<double.Epsilon)
if(Math.Abs(a-b)您的计算是正确的,但是您返回了错误的变量minIndex
,而不是maxIndex
。不要在方法中执行超出必要的操作。这还会计算最小索引和它出现的频率,然后它对结果没有任何影响。这是一个精简版本:
public static int MaxIndex(double[] array)
{
var max = double.MinValue;
int maxInd = -1;
for (int i = 0; i < array.Length; i++)
{
if (max < array[i])
{
max = array[i];
maxInd = i;
}
}
return maxInd;
}
如果代码可读性/可维护性更为重要,并且您不关心几毫秒的增减,那么可以使用LINQ(只枚举一次的版本):
这适用于各种序列和类型,不仅适用于数组和双精度。这不是最吸引人的方式,但也有效
(必须具有正在使用的System.Linq;)
int maxValue=anArray.Max();
int maxIndex=anArray.ToList().IndexOf(maxValue)
参考:Debug。逐步检查代码并查看错误所在。您不应该将double与==“请让我知道我的代码中有什么错误。”您应该告诉我们错误所在。这意味着描述您面临的问题。您是否遇到任何错误?意外结果?之后我们可能会帮助您解决该问题。如果(maxCount>1)返回minIndex;应为maxIndex为什么要计算(也)如果你只想要Max?方法应该只做他们应该做的编码,只有答案通常是没有用的,特别是对初学者来说。考虑添加一个描述,指出问题在哪里以及你是如何修复的,这很好。期待更新。如果好的话,我会高兴地投票:此外,此代码可以替换为一行Console.WriteLine(Array.IndexOf(a,a.Max())
@dennisvan不管DragandDrop说什么,假设您的数组有n
元素。显然,要找到一个Max,您需要执行n
操作(获取元素-检查是否为Max),但您的解决方案是需要n^2
操作(获取最大值-检查当前是否等于最大值),因此对于1000个元素的数组,在最坏的情况下,您的算法需要运行1000000个操作,而不是1000个。99.9%的时间是白白浪费的(999000个操作是无用的)“您的代码通常太复杂,变量太多”同样,就像这个页面上的其他答案一样,你已经错过了问题的要点。也就是说,为什么它不起作用,而不是如何使它更有效率:“只编码答案通常是没有用的,特别是对初学者来说。考虑添加一个描述,指出问题在哪里以及如何修复它。”代码> Array.IndexOf。(numbers,numbers.Max());
这显然是家庭作业,所以你的答案可能不是很有用。它还需要两个循环,而不是OP的循环。我真的不喜欢在只需要迭代一次的情况下迭代集合两次的解决方案…@fubo我冒昧地建议,将方法的速度提高一倍不仅仅是“一点点”性能改进(特别是如果这是家庭作业!)我只想在这里尴尬地指出,如果<代码
public static int MaxIndex(double[] array)
{
var max = double.MinValue;
int maxInd = -1;
for (int i = 0; i < array.Length; i++)
{
if (max < array[i])
{
max = array[i];
maxInd = i;
}
}
return maxInd;
}
public static int MaxIndex(double[] array)
{
if(array.Length == 0)
return -1;
else if (array.Length == 1)
return 0;
double max = array[0];
int maxInd = 0;
for (int i = 1; i < array.Length; i++)
{
if (max < array[i])
{
max = array[i];
maxInd = i;
}
}
return maxInd;
}
int minIndexOfMaxVal = a.Select((num, index) => new {num, index})
.OrderByDescending(x => x.num)
.Select(x => x.index)
.DefaultIfEmpty(-1)
.First();