JosepRivaille

P81966: Cerca dicotòmica

May 23rd, 2015
2,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int posicio(double x, const vector<double>& v, int esq, int dre) {
  7.         if (esq > dre) return -1;
  8.         int mid = (esq+dre)/2;
  9.         if (x < v[mid]) return posicio(x, v, esq, mid-1);
  10.         if (x > v[mid]) return posicio(x, v, mid+1, dre);
  11.         return mid;
  12. }
  13.  
  14. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment