根据范围列表的数字范围 [英] Range of a number according to a range list
问题描述
朋友们,
我有一个数字列表0.25,0.50,0.75,1,1.25,2,2.5,3.5,...然后我需要找出最小范围和最多一个数字。例如我的数字是0.30然后最小范围是0.25,最大值是0.50。
我知道有很多方法可以找到这个但我想知道如果有任何预定义的函数喜欢Icompare。(原因是我想缩短我的代码长度。)
先谢谢。
AFAIK,没有查找范围方法:你必须自己动手。
但它不是大量的代码:取决于你的集合中的数字数量(我注意到它已经排序)它应该我们需要很长时间才能找到你的价值的第一个数字然后那个和之前的指数是范围。
对于我刚刚扫描的少量范围:对于更大的数字,那么二进制印章可能更有效吗?取决于计数,以及您的代码目前正在做什么。
正如我在评论中提到的,您可以使用数组轻松完成此操作。请注意,数组必须按升序排序。
double [] numbers = new [] { 0 。 25 , 0 。 50 , 0 。 75 , 1 , 1 。 25 , 2 , 2 。< span class =code-digit> 5 , 3 。 5 };
int index = Array.BinarySearch(numbers, 0 。 3 跨度>);
if (index > = 0 )
{
Console.WriteLine( 完全匹配 跨度>);
}
其他
{
int lowerBound = -index-2;
int upperBound = -index-1;
if (lowerBound < 0 )
Console.WriteLine( 是最小的);
else if (upperBound > = numbers.Length)
Console.WriteLine( is maximum);
else
Console.WriteLine( 介于{0}和{1}之间,数字[lowerBound],数字[upperBound]);
}
有关 Array.BinarySearch [ ^ ]方法。
您可以使用 Linq [ ^ ]:
double find =。 80 ;
double [] darr = new double [] { 0 。 25 , 0 . 50 , 0 。 75 , 1 , 1 。 25 , 2 , 2 。 5 , 3 。 5 };
double up =( from maxv in darr
其中 maxv > 查找
选择 maxv).First();
double down =( from minv in darr
其中 minv < 查找
选择 minv).Last();
Console.WriteLine( {0}的范围是:{1} - {2},find,down,up);
结果:
0,8的范围是: 0,75-1
Hi friends,
I have a list of numbers 0.25,0.50,0.75,1,1.25,2,2.5,3.5, ... and then I need to find out the range minimum and maximum of a number. For example my number is 0.30 then the min range is 0.25 and maximum is 0.50.
I know there are so many ways to find out this but I want to know that if there is any pre-defined functions like Icompare.(The reason is I want to shorten my code length.)
Thanks in Advance.
AFAIK, there is no "find in range" method: You will have to "do it yourself".
But it's not massive code: depending on the number of numbers in your set (and I notice it's sorted already) it shouldn't take long to find the "first number above" your value and then that and the previous index are the range.
For a small number of ranges I'd just scan up: for a larger number then maybe a binary chop would be more efficient? Depends on the count, and what your code is doing at the moment.
As I mentioned in my comment, you can do this easily with an array. Please note that the array must be sorted in ascending order.
double[] numbers = new[] {0.25,0.50,0.75,1,1.25,2,2.5,3.5}; int index = Array.BinarySearch(numbers, 0.3); if(index >= 0) { Console.WriteLine("is exact match"); } else { int lowerBound = -index-2; int upperBound = -index-1; if(lowerBound < 0) Console.WriteLine("is minimum"); else if(upperBound >= numbers.Length) Console.WriteLine("is maximum"); else Console.WriteLine("is between {0} and {1}", numbers[lowerBound], numbers[upperBound]); }
Please see MSDN for more details on Array.BinarySearch[^] method.
You can achieve it using Linq[^]:
double find = .80; double[] darr = new double[]{0.25,0.50,0.75,1,1.25,2,2.5,3.5}; double up = (from maxv in darr where maxv > find select maxv).First(); double down = (from minv in darr where minv < find select minv).Last(); Console.WriteLine("Range for {0} is: {1}-{2}", find, down, up);
Result:
Range for 0,8 is: 0,75-1
这篇关于根据范围列表的数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!