Advertisement
AndreyKlipikov

Prog. Lab 6. N23

Dec 7th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.     printf("\n  Avtor - Klipikov A.V., student gr. PIbd-11\n");
  5.     printf("Variant N 23\n");
  6.     printf("Dan massiv celih chisel. Nuzhno zamenit' gruppu iz naibolshego kolichestva nulei na maksimalniy element massiva\n");
  7.     printf("Vvedite kolichestvo elementov massiva:\n");
  8.  
  9.     int n, a[10010];
  10.     scanf("%d", &n);
  11.     printf("\n Vvedite %d elementov massiva:\n", n);
  12.     for(int i = 0; i < n; i++)
  13.         scanf("%d", &a[i]);
  14.  
  15.     int max = a[0], nulls = (int)(a[0] == 0), nulls_count = (int)(a[0] == 0), nulls_pos = -1, tmp_count = (int)(a[0] == 0), tmp_pos = (int)(a[0] == 0) - 1;
  16.     for(int j = 1; j < n; j++) {
  17.         if (a[j] > max)
  18.             max = a[j];
  19.  
  20.         if (a[j - 1] != 0 && a[j] == 0) {
  21.             tmp_count = 1;
  22.             tmp_pos = j;
  23.         }
  24.  
  25.         if (a[j - 1] == 0 && a[j] == 0) {
  26.             tmp_count++;
  27.         }
  28.  
  29.         if (a[j - 1] == 0 && (a[j] != 0 || j == (n - 1)) && tmp_count > nulls_count) {
  30.             nulls_count = tmp_count;
  31.             nulls_pos = tmp_pos;
  32.         }
  33.         if (a[j] == 0)
  34.             nulls++;
  35.     }
  36.  
  37.     if (nulls > 0) {
  38.         a[nulls_pos] = max;
  39.         for(int k = (nulls_pos + 1); k < (n - nulls_count + 1); k++)
  40.             a[k] = a[k + nulls_count - 1];
  41.     } else
  42.         nulls_count = 0;
  43.  
  44.     printf("\n Obnovlenniy massiv:\n");
  45.     for(int l = 0; l < n - nulls_count + (int)(nulls > 0); l++) {
  46.         if ((l % 2) == 0) {
  47.             printf("%d ", a[l]);
  48.         }
  49.     }
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement