#include#includeint BinarySearch(int*,int,int,int);int main(){int int_length,int_quer;int start=0,i,num;scanf("%d%d",&int_length,&int_quer);int *ptr=(int*)malloc(sizeof(int)*(int_length));//declare a pointerfor(i=0;iscanf("%d",&ptr[i]);}for(i=1;i<=int_quer;i++){//查詢幾次用一個回圈控制scanf("%d",&num);printf("%d\n",BinarySearch(ptr,start,int_length-1,num));//做二元搜尋(剛宣告陣列的指標,起始,終止,查詢值)}free(ptr);return 0;}//採遞回方式int BinarySearch(int*ptr,int start,int int_length,int num){int min=(start+int_length)/2;if(num==ptr[min]){return min+1;}else if(numreturn BinarySearch(ptr,start,min-1,num);}else if(num>ptr[min]){return BinarySearch(ptr,min+1,int_length,num);}return 0;}
有可能沒有這個值