//创造一个函数,并使之找出1-10之间的7 int binary_search(int ar[], int k, int s) { int left = 0; int right = s - 1; while (left <= right) { int mid = (left + right) / 2; if (ar[mid] > k) { right = mid - 1; } else if (ar[mid] < k) { left = mid + 1; } else { return mid; } } return -1; } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int key = 7; int sz = sizeof(arr) / sizeof(arr[0]); int ret = binary_search(arr,key,sz); if (-1 == ret) { printf("找不到\n"); } else { printf("找到了,下标是:%d\n", ret); } return 0; }