Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int posicio(double x, const vector<double> &v, int esq, int dre){
  5. if(esq > dre) return -1;
  6.  
  7. int mid = (esq + dre)/2;
  8. if(v[mid] == x) return mid;
  9.  
  10. if(esq == dre) return -1;
  11.  
  12.  
  13. if(v[mid] < x) return posicio(x, v, mid+1, dre);
  14. return posicio(x, v, esq, mid);
  15. }
  16.  
  17. int main(){
  18. int n;
  19. cin >> n;
  20. vector <double> V(n);
  21. for(int i=0; i<n; i++) cin >> V[i];
  22. double a;
  23. while(cin >> a) cout << posicio(a, V, 0, V.size()) << endl;
  24. }
  25.  
  26.  
  27. /*
  28. //FUNCIO QUE CALCULA EL FACTORIAL D'UN NOMBRE EN O(n)
  29. int factorial(int n){
  30. if(n == 0) return 1;
  31. return n*factorial(n-1);
  32. }
  33.  
  34. //FUNCIO QUE CALCULA EL Néssim NOMBRE DE FIBONACI EN O(n)
  35. vector <long long> memo (1e5, -1);
  36. long long fibonacci(int n){
  37. if(n == 0) return 0;
  38. if(n == 1) return 1;
  39. if(memo[n] != -1) return memo[n];
  40.  
  41. memo[n] = fibonacci(n-1) + fibonacci(n-2);
  42. return memo[n];
  43. }
  44.  
  45.  
  46.  
  47. //FUNCIO INT PER CALCULAR EL GCD DE DOS NUMEROS
  48. int gcd(int a, int b){
  49. cout << a << " " << b << endl;
  50. if(b == 0) return a;
  51. return gcd(b, a%b);
  52. }
  53.  
  54. //FUNCIO BOOLEANA QUE TROBA SI UN NOMBRE DONAT ES PRIMER EN O(sqrt(n))
  55. bool primer(int num){
  56. if(num < 2) return false;
  57.  
  58. for(int i=2; i*i<=num; i++) if(num%i == 0) return false;
  59. return true;
  60. }
  61.  
  62. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement