Advertisement
AndreyKlipikov

[Segrey] Prog. Lab6. N18

Dec 25th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.         int n, a[10010];
  9.         cout << "Vvedite kolichestvo elementov: " << endl;
  10.         cin >> n;
  11.         cout << "Vvedite elementi massiva:"  << endl;
  12.         for(int i = 0; i < n; i++)
  13.                 cin >> a[i];
  14.  
  15.         int max = a[0], eq = (int)(a[0] == a[1]), eq_count = (int)(a[0] == a[1]), eq_pos = -1, tmp_count = (int)(a[0] == a[1]), tmp_pos = (int)(a[0] == a[1]) - 1;
  16.         for(int j = 1; j < n - 1; j++) {
  17.                 if (a[j] > max)
  18.                         max = a[j];
  19.  
  20.                 if (a[j - 1] != a[j] && a[j] == a[j + 1]) {
  21.                         tmp_count = 1;
  22.                         tmp_pos = j;
  23.                 }
  24.  
  25.                 if (a[j - 1] == a[j]) {
  26.                         tmp_count++;
  27.                         eq++;
  28.                 }
  29.  
  30.                 if (a[j - 1] == a[j] && (a[j] != a[j + 1] || j == (n - 1)) && tmp_count > eq_count) {
  31.                         eq_count = tmp_count;
  32.                         eq_pos = tmp_pos;
  33.                 }
  34.         }
  35.  
  36.         if ((eq_pos + eq_count) == (n - 1) && a[n - 1] == a[n - 2]) {
  37.             eq_count++;
  38.             eq++;
  39.         }
  40.  
  41.         if (eq > 0) {
  42.                 a[eq_pos] = max;
  43.                 for(int k = (eq_pos + 1); k < (n - eq_count + 1); k++)
  44.                         a[k] = a[k + eq_count - 1];
  45.         } else
  46.                 eq_count = 0;
  47.  
  48.         cout << "Obnovlenniy massiv:"  << endl;
  49.         for(int l = 0; l < n - eq_count + (int)(eq > 0); l++)
  50.             cout <<  a[l] << " ";
  51.  
  52.         return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement