从数组中找到最接近的数字 [英] find closest number from array

查看:153
本文介绍了从数组中找到最接近的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListArray在Eg中有数字:

1,1.545,2.43,4,6.7

接下来我有一个十进制变量包含一个数字EG:

1.786


有人可以告诉我如何找到最接近的匹配吗?数字低于arraylist的

十进制变量。


非常感谢提前

I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below the
decimal variable from the arraylist.

Thanks ever so much in advance

推荐答案

迈克,


看起来您的数组已排序。如果是这种情况,那么你可以在$ class上调用静态BinarySearch方法来查找。


你将传递你的数组,以及你的号码。如果结果是

非负数,那么这就是数组中找到的索引

in。如果数字为负数,则等于:


< result> =(<下一个最高值的索引> + 1)* -1


因此,在您的情况下,它将返回-3。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" mike" < SP ****** @ hotmail.com>在消息中写道

新闻:Dl ****************** @ doctor.cableinet.net ...
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
我将ListArray与Eg中的数字相对应:
1,1.546,2.43,4,6.78
接下来我有一个十进制变量,其中包含一个数字EG:
1.786

有人可以请告诉我如何找到最接近的匹配数字低于
来自arraylist的十进制变量。

非常感谢提前
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist.

Thanks ever so much in advance



迈克,


好​​像您的数组已排序。如果是这种情况,那么你可以在$ class上调用静态BinarySearch方法来查找。


你将传递你的数组,以及你的号码。如果结果是

非负数,那么这就是数组中找到的索引

in。如果数字为负数,则等于:


< result> =(<下一个最高值的索引> + 1)* -1


因此,在您的情况下,它将返回-3。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" mike" < SP ****** @ hotmail.com>在消息中写道

新闻:Dl ****************** @ doctor.cableinet.net ...
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
我将ListArray与Eg中的数字相对应:
1,1.546,2.43,4,6.78
接下来我有一个十进制变量,其中包含一个数字EG:
1.786

有人可以请告诉我如何找到最接近的匹配数字低于
来自arraylist的十进制变量。

非常感谢提前
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist.

Thanks ever so much in advance



太棒了,


很抱歉很痛苦,但是当二元搜索产生一个负数时,你能解释我如何找到最近的

匹配的索引吗?真的很困惑?


谢谢

Mike

Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:un ************** @ TK2MSFTNGP10.phx.gbl ...
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest
match when binarysearch produces a negative number i''m really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,
好像您的数组已排序。如果是这种情况,那么你可以在Array类上调用静态BinarySearch方法来查找。

你会传递你的数组以及你的号码。如果结果是非负数,那么这就是数组中找到它的索引。如果数字是负数,则等于:
<结果> =(<下一个最高值的索引> + 1)* -1

因此,在您的情况下,它将返回-3。

希望这会有所帮助。 />
-
- Nicholas Paldino [.NET / C#MVP]
- mv * @ spam .guard.caspershouse.com

" mike" < SP ****** @ hotmail.com>在消息中写道
新闻:Dl ****************** @ doctor.cableinet.net ...
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
我有ListArray号码在例如:
1,1.546,2.43,4,6.78
接下来我有一个十进制变量,其中包含一个数字EG:
1.786

有人可以告诉我如何我找到了最接近的匹配数字低于
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below


来自arraylist的


the

十进制变量。

非常感谢提前
decimal variable from the arraylist.

Thanks ever so much in advance



这篇关于从数组中找到最接近的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆