Advertisement
AndreyKlipikov

[Vlad] Prog Lab 6. N1

Dec 20th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 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 << "\n Vvedite elementi massiva:"  << endl;
  12.         for(int i = 0; i < n; i++)
  13.                 cin >> a[i];
  14.  
  15.         int minus = (int)(a[0] > 0), minus_count = (int)(a[0] > 0), minus_pos = -1, tmp_count = (int)(a[0] > 0), tmp_pos = (int)(a[0] > 0) - 1;
  16.         for(int j = 1; j < n; j++) {
  17.  
  18.                 if (a[j - 1] <= 0 && a[j] > 0) {
  19.                         tmp_count = 1;
  20.                         tmp_pos = j;
  21.                 }
  22.  
  23.                 if (a[j - 1] > 0 && a[j] > 0) {
  24.                         tmp_count++;
  25.                 }
  26.  
  27.                 if (a[j - 1] > 0 && (a[j] <= 0 || j == (n - 1)) && tmp_count > minus_count) {
  28.                         minus_count = tmp_count;
  29.                         minus_pos = tmp_pos;
  30.                 }
  31.         }
  32.  
  33.         for(int i = minus_pos; i < minus_pos + minus_count; i++) {
  34.         int tmp = a[i];
  35.         for(int j = i; j > 0; j--)
  36.             a[j] = a[j - 1];
  37.         a[0] = tmp;
  38.     }
  39.  
  40.     for(int i = 0; i < minus_count / 2; i++) {
  41.         a[i] += a[minus_count - i - 1];
  42.         a[minus_count - i - 1] = a[i] - a[minus_count - i - 1];
  43.         a[i] -= a[minus_count - i - 1];
  44.     }
  45.  
  46.         cout << "\n Obnovlenniy massiv:" << endl;
  47.         for(int i = 0; i < n; i++)
  48.                 cout << a[i] << " ";
  49.        
  50.         return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement