Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*scrieti un program care sa gaseasca nr de elemente ce au exact x cifre de c dintr-un vector folosind metoda DEI*/
- #include <iostream>
- using namespace std;
- void readVar(char varname, int &var) {
- cout << varname << '=';
- cin >> var;
- }
- void readVect(int &n, int v[]) {
- readVar('n', n);
- for (int i = 0; i < n; i++) {
- cout << "v[" << i << "]=";
- cin >> v[i];
- }
- }
- int checkNr(int nr, int x, int c) {
- int cnt = 0;
- while(nr) {
- if (nr % 10 == c)
- cnt++;
- nr /= 10;
- }
- return x == cnt;
- }
- int findElems(int v[], int p, int q, int x, int c) {
- if (p > q)
- return 0;
- int mid = (p + q) / 2;
- return findElems(v, p, mid - 1, x, c) + findElems(v, mid + 1, q, x, c) + checkNr(v[mid], x, c);
- }
- int n, c, x, v[1000];
- int main() {
- readVect(n, v);
- readVar('x', x);
- readVar('c', c);
- cout << findElems(v, 0, n - 1, x, c) << endl;//*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement