找到数组中的最小值 [英] Find the lowest value in an array
问题描述
您好,
我有一个包含4个值的数组(它们包含随机数)。我想
喜欢找到最低价值(如果有领带我想找到一个
领带。)然后删除该值。我是Programming and C#的新手。
感谢您提供的任何帮助
Thomas
ThomasT22写道:
我有一个包含4个值的数组(它们包含随机数)。我想
喜欢找到最低价值(如果有领带我想找到一个
领带。)然后删除该值。我是Programming and C#的新手。
如果它总是4个元素则可能:
minval = Math.Min(Math.Min(a [0 ],a [1]),Math.Min(a [2],a [3]));
Arne
< blockquote>当你说...然后删除那个值。,你的意思是在
过程之后,你应该有一个低值,一个只有三个元素的数组? Lilith和Arne回答了排序问题。
" ThomasT22"写道:
您好,
我有一个包含4个值的数组(它们包含随机数)。我想
喜欢找到最低价值(如果有领带我想找到一个
领带。)然后删除该值。我是Programming and C#的新手。
感谢您提供的任何帮助
Thomas
ModelBuilder写道:
当你说...然后删除那个值。,你的意思是在
之后
进程,你应该有一个低值,一个只有三个元素b $ b元素的数组?
Lilith和Arne回答了排序问题。
是的,但是排序是破坏性的,并且是一种非常低效的方式来找到
中最低的四个数字。由于删除的要求已经明确规定,
我将在这里忽略它。为了找到最低值,我绝对不会使用Sort或Math.Min或Max。
。为了最大限度地提高效率,可以使用以下内容:
int a = x [0];
int b = x [1];
int c = x [2];
int d = x [3];
//有效地a = Math.Min(a,b)
if(b< a)
{
a = b;
}
//有效地a = Math.Min(c,d)
if(d< ; c)
{
c = d;
}
//有效地数学 .Min(a,c)其中总的来说是Math.Min(a,
b,c,d)
返回(a< c)? a:c;
希尔顿
Hello,
I have an array that holds 4 values ( they contain random numbers). I would
like to find the lowest value (if there is a tie i would like to find one of
the tie.) then remove that value. I am new to Programming and C#.
Thanks for any help you can provide
Thomas
ThomasT22 wrote:I have an array that holds 4 values ( they contain random numbers). I would
like to find the lowest value (if there is a tie i would like to find one of
the tie.) then remove that value. I am new to Programming and C#.If it is always 4 elements then maybe:
minval = Math.Min(Math.Min(a[0], a[1]), Math.Min(a[2], a[3]));
Arne
When you say "...then remove that value.", do you mean that after the
process, you should have a low value, and an array with only three elements?
Lilith and Arne answered the sorting issue.
"ThomasT22" wrote:
Hello,
I have an array that holds 4 values ( they contain random numbers). I would
like to find the lowest value (if there is a tie i would like to find one of
the tie.) then remove that value. I am new to Programming and C#.
Thanks for any help you can provide
Thomas
ModelBuilder wrote:When you say "...then remove that value.", do you mean that after the
process, you should have a low value, and an array with only three
elements?
Lilith and Arne answered the sorting issue.Yeah, but sorting is destructive and a very inefficient way to find the
lowest of four numbers. Since the ''remove'' requirement was well specified,
I''m going to ignore it here. To find the lowest value, I definitely
wouldn''t use a Sort or Math.Min or Max. To maximize efficiency, something
along the lines of:
int a = x[0];
int b = x[1];
int c = x[2];
int d = x[3];
// Effectively "a = Math.Min (a, b)"
if (b < a)
{
a = b;
}
// Effectively "a = Math.Min (c, d)"
if (d < c)
{
c = d;
}
// Effectively "Math.Min (a, c)" which in summary is really "Math.Min (a,
b, c, d)"
return (a < c) ? a : c;
Hilton
这篇关于找到数组中的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!