#pragma warning(disable:4996)
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
int key = 7;
int mid = 0;
while (left <= right)
{
mid = left + (right - left) / 2;
if (arr[mid] > key)
{
right = mid - 1;
}
if (arr[mid] < key)
{
left = mid + 1;
}
else
break;
}
if (left <= right)
{
printf("找到了,下标是%d\n", mid);
}
else
{
printf("没找到\n");
}
system("pause");
return 0;
}
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
int key = 7;
int mid = 0;
while (left <= right)
{
mid = left + (right - left) / 2;
if (arr[mid] > key)
{
right = mid - 1;
}
if (arr[mid] < key)
{
left = mid + 1;
}
else
break;
}
if (left <= right)
{
printf("找到了,下标是%d\n", mid);
}
else
{
printf("没找到\n");
}
system("pause");
return 0;
}