C# 实现的二分查找法
public
int
search(
int
[] q)
... {
int i, low = 0, high = q.Length - 1, middle;
Console.Write("请输入想要查找的数字:");
i=int.Parse(Console.ReadLine());
while (low <= high)
...{
middle = (low + high) / 2;
if (i == q[middle])return i;
if (i < q[middle])high = middle - 1;
else low = middle + 1;
}
throw new Exception("数组中不存在这个数。");
}
... {
int i, low = 0, high = q.Length - 1, middle;
Console.Write("请输入想要查找的数字:");
i=int.Parse(Console.ReadLine());
while (low <= high)
...{
middle = (low + high) / 2;
if (i == q[middle])return i;
if (i < q[middle])high = middle - 1;
else low = middle + 1;
}
throw new Exception("数组中不存在这个数。");
}
去年面试时,就被这道题止步了!唉!```不能重蹈覆辙啊!