C# 如何用二分法查找 输入一个数,在已经排序好的数组中查找是否存在和存在于数组什么位置 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 设定x,y分别标注查找的开始位置和结束位置,然后如(x+y)/2这个中间元素比较,如果(x+y)/2>带查找元素(a)那么就在x--y(y = (x+y)/2)下查找反之则在x(x = (x+y)/2)--y下查找 这么简单的 int function(int a[] ,int b){ } namespace BinaryTree{ class Program { static int BinaryFind(int[] a, int b) { int low = 1; int high = a.Length; int mid; while (low <=high) { mid = (low + a.Length)/2; if (a[mid] > b) high = mid-1; else if (a[mid] < b) low = mid+1; else return mid+1; } return 0; } static void Main(string[] args) { int[] a = new int[5] { 4, 6,9, 12, 18 }; int store; store = Program.BinaryFind(a,12); if (Convert.ToBoolean(store)) Console.WriteLine("{0}", store); else Console.WriteLine("没有要查找的数字"); } }} } static void Main(string[] args) { int[] a = new int[5] { 4, 6, 19, 2, 8 }; int store; store = Program.BinaryFind(a,8); if (Convert.ToBoolean(store)) Console.WriteLine("{0}", store); else Console.WriteLine("没有要查找的数字"); } } http://blog.csdn.net/fengyarongaa/article/details/6563184 震动问题 如何获得GridView中的FooterTemplate的控件的值? .net中如何合并多个表结构一样的excel 最简单问题(除法运算) 如何获取方法所在的命名空间(C#) 错在哪? (在线)高分求在C#web中如何里实现Datagrid的打印预览和打印? 接口的作用 各位大哥请帮帮我,如何将下面的vbs转换到c#,不胜感激! 哪里有调用系统API的资料? 如何用PdfReader打开带密码的PDF 求winform 下实现画折线(曲线)图的代码
如果(x+y)/2>带查找元素(a)那么就在x--y(y = (x+y)/2)下查找
反之则在x(x = (x+y)/2)--y下查找 这么简单的
{
}
{
class Program
{
static int BinaryFind(int[] a, int b)
{
int low = 1;
int high = a.Length;
int mid;
while (low <=high)
{
mid = (low + a.Length)/2;
if (a[mid] > b)
high = mid-1;
else if (a[mid] < b)
low = mid+1;
else
return mid+1;
}
return 0; }
static void Main(string[] args)
{
int[] a = new int[5] { 4, 6,9, 12, 18 };
int store;
store = Program.BinaryFind(a,12);
if (Convert.ToBoolean(store))
Console.WriteLine("{0}", store);
else
Console.WriteLine("没有要查找的数字");
}
}
}
}
static void Main(string[] args)
{
int[] a = new int[5] { 4, 6, 19, 2, 8 };
int store;
store = Program.BinaryFind(a,8);
if (Convert.ToBoolean(store))
Console.WriteLine("{0}", store);
else
Console.WriteLine("没有要查找的数字");
}
}