SHARE
TWEET

Untitled

a guest Nov 15th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <malloc.h>
  6. #include <cstdlib>
  7.  
  8. #define random_r() ( rand() % ( max - min + 1 ) + min )
  9.  
  10. int main(void)
  11. {
  12.    
  13.    setlocale(0, "");
  14.    
  15.     int N = 20;
  16.     int min = -100;
  17.     int max = 100;
  18.    
  19.     int array[N];
  20.     int i, max_head, max_length, max_sum, current_head, current_length, current_sum;
  21.    
  22.     srand(time(NULL));
  23.    
  24.     for ( i = 0; i < N; ++i )
  25.     printf("array[%d] = ", i);
  26.     scanf("%d", &array[i]);
  27.    
  28.     printf("Âñå ýëåìåíòû ìàññèâà:\n");
  29.     for ( i = 0; i < N; ++i ) {
  30.         printf("%d ", array[i]);
  31.         scanf("%d", &array[i]);
  32.     }
  33.        
  34.    
  35.     max_head = current_head = -1;
  36.     max_sum = current_sum = 0;
  37.     max_length = current_length = 0;
  38.    
  39.     for ( i = 0; i <= N; ++i ) {
  40.         if ( i == N || array[i] <= 0 ) {
  41.             if ( current_head >= 0 ) {
  42.                 if ( current_sum > max_sum )
  43.                 {
  44.                     max_head = current_head;
  45.                     max_length = current_length;
  46.                     max_sum = current_sum;
  47.                 }
  48.                 current_head = -1;
  49.                 current_length = 0;
  50.                 current_sum = 0;
  51.             }
  52.         }
  53.         else {
  54.             if ( current_head < 0 )
  55.                 current_head = i;
  56.             current_length += 1;
  57.             current_sum += array[i];
  58.         }
  59.     }
  60.    
  61.     printf("\nÏåðâàÿ ïîëîæèòåëüíàÿ ïîñëåäîâàòåëüíîñòü ñ max_sum:\n");
  62.     for ( i = max_head; i < max_head + max_length; ++i )
  63.         printf("%d ", array[i]);
  64.     printf("\nSum: %d\n", max_sum);
  65.    
  66.     return 0;
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top