/*先把中間元素找到,尋找的元素在中間元素左邊, 則小於中間元素,改變right。 再繼續折半找中間元素尋找*/ #include<stdio.h> #include<string.h> int zheban(int x,char arr[],int left,int right) { while(left<=right) { int mid=left-(left-right)/2; if(x > arr[mid]) { left=mid+1; } else if(x < arr[mid]) { right=mid-1; } else { return 1; } } return 0; } int main() { int mid=0; char arr[]={1,3,4,6,7,8,56,67,78}; int ret=0; int size=0; int num=0; size=strlen(arr); ret=zheban(5,arr,0,size-1); if(ret==0) { printf("not exsit"); } else { printf("%d",arr[mid]); } return 0; }