c# - 如何确定数字位于哪两个数组索引之间

标签 c# arrays

我有一些我编写过的代码可以正常工作,但我觉得我在用一种非常迟钝的方式来编写它。

我有一个包含 13 个元素的固定大小的数组。该数组是从 SQL 表填充的。如果给定一个数字,我想知道该数字属于哪两个数组索引之间。

例如:如果我的号码是500,我有以下元素: -500, -400, -255, -89, 77, 243, 409, 575, 741, 907, 1073, 1500, 2000

...500 将在元素 6 和 7 之间。(409 和 575)

我已经使用 IF 语句编写了一个相当蛮力的方法来执行此操作,代码如下。我主要是创建第二个数组,然后使用 IF 语句来检查第一个数组中的数字。注意 - 我忽略了数字大于最大或最小数组数字的情况。我已经搜索了一种可以在索引之间搜索的数组方法,但无济于事。关于如何更有效地执行此操作的任何想法?谢谢。

int[] deviations = new int[13] { -500, -400, -255, -89, 77, 243, 409, 575, 741, 907, 1073, 1500, 2000 };
int[] arrayCheck = new int[12];
Int64 num = 500;
Console.WriteLine("The number is: {0}", num);

if ((num >= deviations[0]) && (num < deviations[1]))
{arrayCheck[0] = 1;}
else { arrayCheck[0] = 0;}

if ((num >= deviations[1]) && (num < deviations[2]))
{arrayCheck[1] = 1;}
else { arrayCheck[1] = 0;}

if ((num >= deviations[2]) && (num < deviations[3]))
{ arrayCheck[2] = 1; }
else { arrayCheck[2] = 0; }

if ((num >= deviations[3]) && (num < deviations[4]))
{ arrayCheck[3] = 1; }
else { arrayCheck[3] = 0; }

if ((num >= deviations[4]) && (num < deviations[5]))
{ arrayCheck[4] = 1; }
else { arrayCheck[4] = 0; }

if ((num >= deviations[5]) && (num < deviations[6]))
{ arrayCheck[5] = 1; }
else { arrayCheck[5] = 0; }

if ((num >= deviations[6]) && (num < deviations[7]))
{ arrayCheck[6] = 1; }
else { arrayCheck[6] = 0; }

if ((num >= deviations[7]) && (num < deviations[8]))
{ arrayCheck[7] = 1; }
else { arrayCheck[7] = 0; }

if ((num >= deviations[8]) && (num < deviations[9]))
{ arrayCheck[8] = 1; }
else { arrayCheck[8] = 0; }

if ((num >= deviations[9]) && (num < deviations[10]))
{ arrayCheck[9] = 1; }
else { arrayCheck[9] = 0; }

if ((num >= deviations[10]) && (num < deviations[11]))
{ arrayCheck[10] = 1; }
else { arrayCheck[10] = 0; }

if ((num >= deviations[11]) && (num < deviations[12]))
{ arrayCheck[11] = 1; }
else { arrayCheck[11] = 0; }

int arrayIndex = Array.IndexOf(arrayCheck, 1);
Console.WriteLine("The num is between array indexes: {0} and {1}", arrayIndex, arrayIndex + 1);

最佳答案

如果数组已排序,Array.BinarySearch是要走的路。

int[] deviations = new int[13] { -500, -400, -255, -89, 77, 243, 409, 575, 741, 907, 1073, 1500, 2000 };
var index = Array.BinarySearch(deviations,500);
if(index >= 0)
{
    //Found at index 
}   
else
{
    int expectedindex = ~index;
    //Should fall in expectedindex 
}

Array.BinarySearch returns the index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).

所以当这个方法返回一个负值时,这意味着给定的值没有找到。但是bit-wise complement返回元素的预期索引。按位补码的工作原理是将 1 的位表示更改为 0,将 0 更改为 1,我们使用 ~ operator将按位补码的数字变回原来的。

关于c# - 如何确定数字位于哪两个数组索引之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978502/

相关文章:

c# - WPF RichTextBox 获取字符矩形

c# - ASP.NET MVC 重定向 OnEnd

c++ - 结构类型元素数组的问题

javascript - 如何在 Javascript 中按键获取值

c - 两个大数 char 类型的最高有效位之和

java - 原始数据类型数组与原始数据类型操作

c# - 一次性在多个表中添加数据,并在它们之间设置外键约束

c# - Kendo DatePicker Max 和 Min 值不限制文本输入

arrays - Swift 库中的数组问题 - Realm 2.4.1

c# - 关闭或处置读卡器时一切都会卡住