判断是否没找到条件不对,改一下:
#include
int fun(int a[], int m, int n, int p)
{
int mid;
if (m > n)return -1;
mid = (m + n) / 2;
if (a[mid] == p)
return mid;
else if (a[mid]
{
return fun(a, mid + 1, n, p);
}
else if (a[mid]>p)
{
//m = m - m / 2;
return fun(a, m, mid - 1, p);
}
//else if (((mid == m) & a[m] != p) || ((mid + 1) == n) & a[n] != p)
// return -1;
}
void main()
{
int p;
int array[10] = { 1, 2, 5, 10, 17, 26, 37, 50, 65, 82 };
scanf("%d", &p);
printf("%d", fun(array, 0, 9, p));
}