//2017年7月22日 #include <stdio.h> #define LEN 15 int main(){ int n; void binSearch(int * p,int n); int arr[LEN]={98,97,88,87,76,70,65,54,43,42,39,36,35,31,29}; printf("请输入要查找的数:\n"); scanf("%d",&n); binSearch(arr,n); return 0; } void binSearch(int * p,int n){ int low,high,mid; low=0; high=LEN-1; while(low<=high){ mid=(low+high)/2; if(p[mid]==n) {printf("%d是数组的第%d个元素的值(下标从0开始)\n",n,mid); break;//注意找到该元素之后,就结束循环 } else if(p[mid]>n) low = mid+1; else high = mid-1; } if(low>high) printf("数组中无此数"); }
时间: 2024-10-12 17:09:04