AlexandruT

Stergerea tuturor aparitiilor lui x din vectorul a

Oct 29th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int a[20];
  8.  
  9. int main()
  10. {
  11.     int x, n, j, i, p;
  12.     n = 19;
  13.     // generare random
  14.     srand(time(0));
  15.     for(i = 0; i <= n; i++) a[i] = rand() % 3;
  16.     // afisare
  17.     for(i = 1; i <= n; i++) cout << a[i] << " ";
  18.     cout << "\n";
  19.  
  20.     x = 2;
  21.     // elimin pe x din a
  22.     // complexitate O(n*n)
  23.     for(i = 1; i <= n; )
  24.     {
  25.         if(a[i] == x)
  26.         {
  27.             p = i;
  28.             for(j = p + 1; j <= n; j++) a[j - 1] = a[j];
  29.             n--;
  30.         }
  31.         else i++;
  32.     }
  33.     for(i = 1; i <= n; i++) cout << a[i] << " ";
  34.     cout << "\n";
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment