Advertisement
Gamerkin

1.1

Mar 24th, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void delFirstMetod(int x[], int& n, int key)
  7. {
  8.     int i = 0;
  9.     int comparisons = 0, movements = 0;
  10.  
  11.     while (i <= n)
  12.     {
  13.         if (x[i] == key)
  14.         {
  15.             ++comparisons;
  16.             //удаление
  17.             for (int j = i; j <= n - 1; ++j)
  18.             {
  19.                 x[j] = x[j + 1];
  20.                 ++movements;
  21.             }
  22.             n -= 1;
  23.         }
  24.  
  25.         else
  26.         {
  27.             i += 1;
  28.  
  29.         }
  30.  
  31.     }
  32.     cout << "Количество сравнений: " << comparisons << endl;
  33.     cout << "Количество перемещений: " << movements << endl;
  34.  
  35. }
  36.  
  37. void delOtherMetod(int x[], int& n, int key)
  38. {
  39.     int j = 0;
  40.  
  41.     int comparisons = 0, movements = 0;
  42.  
  43.     for (int i = 1; i < n; ++i)
  44.     {
  45.         x[j] = x[i];
  46.         ++movements;
  47.         if (x[i] != key)
  48.         {
  49.             ++j;
  50.             ++comparisons;
  51.         }
  52.     }
  53.     n = j;
  54.     cout << "Количество сравнений: " << comparisons << endl;
  55.     cout << "Количество перемещений: " << movements << endl;
  56. }
  57.  
  58. void Randomazer(int x[], int n)
  59. {
  60.     for (int i = 0; i < n; i++)
  61.     {
  62.         x[i] = rand() % 10;
  63.     }
  64. }
  65.  
  66. void Print(int x[], int n) {
  67.     for (int i = 0; i < n; ++i)
  68.     {
  69.         cout << x[i] << " ";
  70.     }
  71.     cout << endl;
  72. }
  73.  
  74. int main()
  75. {
  76.     setlocale(0, "");
  77.     int n, key;
  78.  
  79.     cout << "Размер массива: 10 или 100?" << endl;
  80.     cin >> n;
  81.     auto* x = new int[n];
  82.  
  83.     //Randomazer(x, n);
  84.  
  85.     for (int i = 0; i < n; i++)
  86.     {
  87.         int el;
  88.         cout << "Введите элемент #" << i + 1 << ": "; cin >> el;
  89.         x[i] = el;
  90.     }
  91.  
  92.     cout << "Ваш массив" << endl;
  93.     Print(x, n);
  94.     cout << "Длина массива = " << n << endl;
  95.  
  96.     cout << "Введите элемент, который хотите удалить: "; cin >> key;
  97.  
  98.     delOtherMetod(x, n, key);
  99.  
  100.     cout << "Очищенный массив" << endl;
  101.     Print(x, n);
  102.     cout << "Длина массива = " << n << endl;
  103.  
  104.     delete[]x;
  105.  
  106.     system("pause");
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement