|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#define M 10
int main()
{
int n,low,high,found,mid,a[M] = {1,2,3,4,5,6,7,8,9,10};
low = 0;high = M-1;found = 0;
printf("Please input the search number: ");
scanf("%d",&n);
while(low <= high)
{
mid = (low + high)/2;
if(a[mid] == n)
{
found = 1;
break;
}
else
{
if(a[mid] > n)
{
high = mid-1;
}
else
{
low = mid+1;
}
}
}
if(found == 1)
{
printf("The number is:%d\n",a[mid]);
}
else
{
printf("The number is not search!!!");
}
return 0;
} |
|