【模板】二分
1 int binsearch(int *t,int k,int n) 2 { 3 //t为数组,k是要查找的数,n为长度,此为升序 4 5 int low = 1,high = n,mid; 6 7 while(low<=high) 8 { 9 mid = (low+high)/2; 10 if(k == t[mid]) 11 return mid; 12 else if(k<t[mid]) 13 high = mid-1; 14 else 15 low = mid+1; 16 } 17 18 return -1; 19 20 }
时间: 2024-10-06 00:48:53