Advertisement
Wolfrost

prova tizzio lol

Jul 1st, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std; //who cares right
  4.  
  5. //"Find the length of the longest non-decreasing sequence"
  6.  
  7. int main()
  8. {
  9.     int ArraySize;
  10.     cout << "Size of the array: " << endl;
  11.     cin >> ArraySize;
  12.     if (ArraySize <= 0 || ArraySize >= 10000)
  13.     {
  14.         cout << "Invalid array size, can't continue." << endl;
  15.         return 1;
  16.     }
  17.    
  18.     int* MyValues = new int[ArraySize];
  19.    
  20.     for (int i=0; i<ArraySize; i++)
  21.         MyValues[i] = rand() % 100;
  22.        
  23.     int MaxLength = 0;
  24.     int Len = 0;
  25.    
  26.     for (int i=1; i<ArraySize; i++)
  27.     {
  28.         if (MyValues[i] >= MyValues[i-1])
  29.         {
  30.             // The sequence is still increasing
  31.             Len++;
  32.         }
  33.         else
  34.         {
  35.             // The sequence stops
  36.             if (Len > MaxLength) // Check for MaxLength
  37.                 MaxLength = Len;
  38.             Len = 0;
  39.         }
  40.     }
  41.    
  42.     cout << "The length of the longest non-decreasing sequence should be " << MaxLength << endl;
  43.    
  44.     delete [] MyValues;
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement