Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int posicio(double x, const vector<double> &v, int esq, int dre){
- if(esq > dre) return -1;
- int mid = (esq + dre)/2;
- if(v[mid] == x) return mid;
- if(esq == dre) return -1;
- if(v[mid] < x) return posicio(x, v, mid+1, dre);
- return posicio(x, v, esq, mid);
- }
- int main(){
- int n;
- cin >> n;
- vector <double> V(n);
- for(int i=0; i<n; i++) cin >> V[i];
- double a;
- while(cin >> a) cout << posicio(a, V, 0, V.size()) << endl;
- }
- /*
- //FUNCIO QUE CALCULA EL FACTORIAL D'UN NOMBRE EN O(n)
- int factorial(int n){
- if(n == 0) return 1;
- return n*factorial(n-1);
- }
- //FUNCIO QUE CALCULA EL Néssim NOMBRE DE FIBONACI EN O(n)
- vector <long long> memo (1e5, -1);
- long long fibonacci(int n){
- if(n == 0) return 0;
- if(n == 1) return 1;
- if(memo[n] != -1) return memo[n];
- memo[n] = fibonacci(n-1) + fibonacci(n-2);
- return memo[n];
- }
- //FUNCIO INT PER CALCULAR EL GCD DE DOS NUMEROS
- int gcd(int a, int b){
- cout << a << " " << b << endl;
- if(b == 0) return a;
- return gcd(b, a%b);
- }
- //FUNCIO BOOLEANA QUE TROBA SI UN NOMBRE DONAT ES PRIMER EN O(sqrt(n))
- bool primer(int num){
- if(num < 2) return false;
- for(int i=2; i*i<=num; i++) if(num%i == 0) return false;
- return true;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement