#define N 10
/*
int main() {
int a[]={0,1,2,3,4,5,6,7,8,9},k
int low = 0,high = N - 1,mid,find = 0
printf("请输入欲查找的值:\n")
scanf("%d",&k)
while(low <= high) {
mid = (low + high) / 2
if(a[mid] == k) {
printf("找到位置为:%d\n",mid + 1)
find = 1
break
}
if(a[mid] > k) high = mid - 1
else low = mid + 1
}
if(!find) printf("%d未找到\n",k)
return 0
}
*/
int main() {
int a[]={0,1,2,3,4,5,6,7,8,9},k
int *low = a,*high = a + N - 1,*mid,find = 0
printf("请输入欲查找的值:\n")
scanf("%d",&k)
while(low <= high) {
mid = a + (high - a + low - a) / 2
if(*mid == k) {
printf("找到位置为:%d\n",mid + 1 - a)
find = 1
break
}
if(*mid > k) high = mid - 1
else low = mid + 1
}
if(!find) printf("%d未找到\n",k)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)