求小于4的数
#include<stdio.h>int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr);
int i = 5;
int left = 0;
int right = sz - 1;
int mid = (left + right) / 2;
while(left <= right)
{
if (arr < i)
{
left = mid + 1;
}
if (arr > i)
{
right = mid - 1;
}
else if (arr = i)
{
printf("你所寻找的数字为;%d", arr);
break;
}
}
if (left > right)
{
printf("你所寻找的数字不在数组中");
}
return 0;
}
我这个代码就只能找大于4的数,小于4都会死循环,希望大家能帮我修改一下 把mid放到循环里面
#include<stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr);
int i = 4;
int left = 0;
int right = sz - 1;
while(left <= right)
{
int mid = (left + right) / 2;
if (arr < i)
{
left = mid + 1;
}
if (arr > i)
{
right = mid - 1;
}
else if (arr = i)
{
printf("你所寻找的数字为;%d", arr);
break;
}
printf("1");
}
if (left > right)
{
printf("你所寻找的数字不在数组中");
}
return 0;
}
页:
[1]