今天笔试,有道写二分查找的题,居然写错了,该死
1 int bs2(int *a, int l, int u, int x)
2 {
3 int m;
4 while (l <= u)
5 {
6 m = (l + u) / 2;
7 if (a[m] > x)
8 u = m - 1;
9 else if (a[m] < x)
10 l = m + 1;
11 else
12 return m;
13 }
14 return -1;
15 }
16
17 int main()
18 {
19 int a[] = {1, 2, 4, 6, 8, 9};
20 int l = bs2(a, 0, 5, 2);
21 printf("%d\n", l);
22 return 0;
23 }
之前看到一贴说多少比例的人不能正确的写出,还真的是呐