fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int bsearch(int a[], int low, int high, int x){
  5. int mid = low + (high-low)>>1;
  6. if (low>high){
  7. return -1;
  8. }
  9. if(a[mid]==x){
  10. return mid;
  11. }
  12. else if(a[mid] > x){
  13. return bsearch(a, low, mid-1, x);
  14. }
  15. else{
  16. return bsearch(a, mid+1, high, x);
  17. }
  18.  
  19. }
  20.  
  21. int main(){
  22. int a[] = {4,5,24,42,223,1113,2203};
  23. int x; cin>>x;
  24. cout<<bsearch(a, 0, sizeof(a)/sizeof(a[0]), x);
  25.  
  26. return 0;
  27.  
  28. }
Success #stdin #stdout 0s 5648KB
stdin
42
stdout
3