Advertisement
Toxotsist

Сиаод 1

Feb 28th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. int Random(int alg)
  6. {
  7.   int x = rand() % alg;
  8.   return x;
  9. }
  10.  
  11. void delFirstMetod(int* x,int n,int key)
  12. {
  13.   clock_t start, stop;
  14.   start = clock ();
  15.   int i = 0;
  16.   while (i < n) {
  17.     if (x[i] == key) {
  18.       for (int j = i; j<n-1; j++){
  19.         x[j] = x[j+1];
  20.       }
  21.       n=n-1;
  22.     }
  23.     else {
  24.       i = i+1;
  25.     }
  26.   }
  27.   n = i;
  28.   cout << n << ":";
  29.  for (int k = 0; k < n; k++) {cout << x[k] << " ";}
  30.  cout << endl;
  31.  stop = clock();
  32.  cout << stop - start << endl;
  33. }
  34.  
  35. void delOtherMetod(int* x,int n,int key, int alg)
  36. {
  37.   clock_t start, stop;
  38.   start = clock ();
  39.  int j = 0;
  40.  for (int i = 0; i < n; i++) {
  41.     x[j] = x[i];
  42.     if ((x[i] != key)){
  43.      j++;
  44.     }
  45.  }
  46.  n = j;
  47.  cout << n << ":";
  48.  for (int k = 0; k < n; k++) {cout << x[k] << " ";}
  49.  cout << endl;
  50.  stop = clock();
  51.  cout << stop - start;
  52. }
  53.  
  54. int main()
  55. {
  56.     setlocale(LC_ALL, "Russian");
  57.     int n, key, alg;
  58.     cout << "Vvedite kolichestvo elements:" << endl;
  59.     cin >> n;
  60.     cout << "Vvedite diapozon randomizera:" << endl;
  61.     cin >> alg;
  62.     cout << "Vvedite key:" << endl;
  63.     int* x = new int[n]; //{1,2,3,2,2,2,5,2,2,2};
  64.     for (int i = 0; i<n; i++){
  65.       x[i]=Random(alg);
  66.     }
  67.     cin >> key;
  68.     delFirstMetod(x,n,key);
  69.     delOtherMetod(x, n, key, alg);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement