Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <math.h>
- using namespace std;
- int main() {
- int n, a[10010];
- cout << "Vvedite kolichestvo elementov: " << endl;
- cin >> n;
- cout << "Vvedite elementi massiva:" << endl;
- for(int i = 0; i < n; i++)
- cin >> a[i];
- 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;
- for(int j = 1; j < n - 1; j++) {
- if (a[j] > max)
- max = a[j];
- if (a[j - 1] != a[j] && a[j] == a[j + 1]) {
- tmp_count = 1;
- tmp_pos = j;
- }
- if (a[j - 1] == a[j]) {
- tmp_count++;
- eq++;
- }
- if (a[j - 1] == a[j] && (a[j] != a[j + 1] || j == (n - 1)) && tmp_count > eq_count) {
- eq_count = tmp_count;
- eq_pos = tmp_pos;
- }
- }
- if ((eq_pos + eq_count) == (n - 1) && a[n - 1] == a[n - 2]) {
- eq_count++;
- eq++;
- }
- if (eq > 0) {
- a[eq_pos] = max;
- for(int k = (eq_pos + 1); k < (n - eq_count + 1); k++)
- a[k] = a[k + eq_count - 1];
- } else
- eq_count = 0;
- cout << "Obnovlenniy massiv:" << endl;
- for(int l = 0; l < n - eq_count + (int)(eq > 0); l++)
- cout << a[l] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement