Advertisement
STANAANDREY

12/10/2020clasa3

Oct 12th, 2020 (edited)
2,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. /*scrieti un program care sa gaseasca nr de elemente ce au exact x cifre de c dintr-un vector folosind metoda DEI*/
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void readVar(char varname, int &var) {
  6.     cout << varname << '=';
  7.     cin >> var;
  8. }
  9.  
  10. void readVect(int &n, int v[]) {
  11.     readVar('n', n);
  12.     for (int i = 0; i < n; i++) {
  13.         cout << "v[" << i << "]=";
  14.         cin >> v[i];
  15.     }
  16. }
  17.  
  18. int checkNr(int nr, int x, int c) {
  19.     int cnt = 0;
  20.     while(nr) {
  21.         if (nr % 10 == c)
  22.             cnt++;
  23.         nr /= 10;
  24.     }
  25.     return x == cnt;
  26. }
  27.  
  28. int findElems(int v[], int p, int q, int x, int c) {
  29.     if (p > q)
  30.         return 0;
  31.     int mid = (p + q) / 2;
  32.     return findElems(v, p, mid - 1, x, c) + findElems(v, mid + 1, q, x, c) + checkNr(v[mid], x, c);
  33. }
  34.  
  35. int n, c, x, v[1000];
  36. int main() {
  37.     readVect(n, v);
  38.     readVar('x', x);
  39.     readVar('c', c);
  40.     cout << findElems(v, 0, n - 1, x, c) << endl;//*/
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement