[英]adding maximum and minimum values in a 2D array, excluding the highest and lowest integers
I have a 2D array and I want to output the sum of the values for each array individually but also exclude the maximum and minimum values from the calculation of that sum. 我有一个2D数组,我想分别输出每个数组的值的总和,但也要从该总和的计算中排除最大值和最小值。 EG if array = { {2, 3, 4, 5}, {4, 5, 6, 7} }.
如果数组= {{2,3,4,5},{4,5,6,7}},则为EG。 then the code should exclude 2 and 5 from the first array and just add and output 9 (3 + 4), and then output 11 for the second array (5 + 6).
那么代码应从第一个数组中排除2和5,仅相加并输出9(3 + 4),然后为第二个数组输出11(5 + 6)。 Also if the array contains 2 ore more of the lowest value and two or more of the highest value it should exclude only 1 of those however many similar values EG if the array = {2, 2, 3, 4, 5, 6, 6} the code should output 20 (2 + 3 + 4 + 5 + 6).
同样,如果数组包含2个或多个最小值,而两个或多个包含最大值,则如果数组= {2,2,3,4,5,6,6 }代码应输出20(2 + 3 + 4 + 5 + 6)。
I have attempted coding this myself however my values are not showing up. 我本人尝试对此进行编码,但是我的值没有显示出来。 I think it might have something to do with my scores_total equalling 0 but I don't know how else to declare it.
我认为这可能与我的scores_total等于0有关,但我不知道该如何声明。
any help would be appreciated. 任何帮助,将不胜感激。
results array is stored in main 结果数组存储在main中
static void JudgesTotal(int[,] results)
{
int Minimum = results[0, 0];
int Maximum = results[0, 0];
int score_total = 0;
for (int i = 0; i < results.GetLength(0); i++)
{
score_total = score_total + results[i, j];
if (Minimum > results[i, j])
{
Minimum = results[i, j];
}
if (Maximum < results[i, j])
{
Maximum = results[i, j];
}
score_total = score_total - (Maximum - Minimum);
if (results[i, j] > results[i, j] + 1)
{
Console.Write("{0}", score_total);
}
}
}
There are a number of problems with your code. 您的代码有很多问题。 First you don't reset the
Minimum
and Maximum
at the start of the next array. 首先,您无需在下一个数组的开始处重置“
Minimum
和“ Maximum
”。 Second you don't reset the score_total
at the start of each new array. 其次,您无需在每个新数组的开始处重置
score_total
。 Next, your comparison for the max value is wrong. 接下来,您对最大值的比较是错误的。 And finally you have to subtract out the min and max after the inner loop finishes.
最后,在内循环完成之后,您必须减去最小值和最大值。 Here's an example of how to fix all those issues.
这是解决所有这些问题的示例。 Also I'm not clear on why you had
if(result[i,j] > result[i,j] + 1)
in there since it would only be true
if result[i,j]
was int.MaxValue
. 我也不清楚你为什么在其中有
if(result[i,j] > result[i,j] + 1)
,因为只有在result[i,j]
是int.MaxValue
result[i,j]
它才会为true
。
for(int i = 0; i < results.GetLength(0); i++)
{
if(results.GetLength(1) <= 2)
{
//There are either 0 items which would sum to 0
//Or 1 item which is both the min and max and the sum should be 0
//Or 2 items where one is the min and the other the max and the sum should be 0
Console.WriteLine(0);
continue;
}
long min = results[i,0];
long max = results[i,0];
long total = 0;
for(int j = 0; j < results.GetLength(1); j++)
{
total += results[i,j];
if(results[i,j] < min)
min = results[i,j];
if(max < results[i,j])
max = results[i,j];
}
total -= (min + max);
Console.WritLine(total);
}
Note I made the min
, max
, and the total
of type long
to avoid potential overflows. 注意,我将
min
, max
和total
的类型设为long
以避免潜在的溢出。 total
because summing up enough integers or large integers could result in a value that doesn't fit in an int
. total
因为将足够的整数或大整数求和可能导致一个不适合int
。 Worse case is an array of size int.MaxValue
where all the items are int.MaxValue
and the total
woudl be int.MaxValue * int.MaxValue
and that will fit in long
. 更糟糕的情况是一个大小为
int.MaxValue
的数组,其中所有项目均为int.MaxValue
, total
int.MaxValue * int.MaxValue
为int.MaxValue * int.MaxValue
,并且将适合long
。 The min
and max
are long
because we add them and int.MaxValue + int.MaxValue
doesn't fit in int
, but does fit in long
. min
和max
很long
因为我们将它们相加,并且int.MaxValue + int.MaxValue
不适合int
,但是适合long
。 Also the same ideas holds for int.MinValue
as well. 同样的想法也适用于
int.MinValue
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.