Advertisement
STANAANDREY

12/10/2020cls 2

Oct 12th, 2020 (edited)
2,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /*scrieti un program ce sa gaseasca nr de elemente mai mari ca x dintr-un tablou unidimensional cu metoda DEI*/
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void read(int &n, int v[]) {
  6.     cout << "n=";cin >> n;
  7.     for (int i = 0; i < n; i++) {
  8.         cout << "v[" << i << "]=";
  9.         cin >> v[i];
  10.     }
  11. }
  12.  
  13. int check(int v[], int p, int q, int x) {
  14.     if (p > q)
  15.         return 0;
  16.     int mid = (p + q) / 2;
  17.     return check(v, p, mid - 1, x) + check(v, mid + 1, q, x) + (v[mid] > x);
  18. }
  19.  
  20. int main() {
  21.     int n, v[1000], x;
  22.     read(n, v);
  23.     cout << "x=";cin >> x;
  24.     cout << check(v, 0, n - 1, x);
  25.     return 0;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement