|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
int main()
{
int a[10] = {-12,0,6,16,23,56,80,100,110,115};
int i,num;
int low = 0,high = 9,mid = 4;
printf("Please input a number :");
scanf("%d",&num);
for(mid = 4;;)
{
if (num > a[mid])
{
low = mid + 1;
mid = (low + high) / 2;
}
else if (num < a[mid])
{
high = mid - 1;
mid = (low + high) / 2;
}
else if (num == a[mid] || num == a[high])
{
printf("The number is in this array,and it`s number %d",mid + 1);
break;
}
if (low > high)
{
printf("This number is not in this array.");
break;
}
}
}
这个是小甲鱼老师C语言视频的第30节所说的利用二分法查找数组中的元素,我自己的一种写法,有大神指点指点吗,总感觉不太好,没有老师的上档次 |
|