#include<stdio.h> int main(void) { int arr[]={0,1,2,3,14,15,16,27,28,29},left,right,key,flag,mid; printf("Please input the number you want to search:\t"); scanf("%d",&key); left=0; right=9; while(left<=right) { mid=(left+right)/2; if(arr[mid]==key) { flag=1; break; } else if(key>arr[mid]) { left=mid+1; } else { right=mid-1; } } if(flag==1) { printf("The number %d is arr[%d] in the array.",key,mid); } else { printf("There is no %d in the array.\n",key); } return 0; }
by: 发表于:2017-08-15 10:52:45 顶(0) | 踩(0) 回复
??
回复评论