coasterka

MaxSequenceOfArrElements

Mar 10th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. //максималната поредица от нарастващи елементи в масив arr[n]. Елементите може и да не са последователни.
  2.  
  3. static void Main(string[] args)
  4.     {
  5.         int[] array = new int[] { 6, 9, 2, 3, 8, 7, 6, 5, 8, 4 };//==> 2, 3, 5, 8
  6.  
  7.         int bestLength = 0;
  8.         int[] bestResult = new int[array.Length];
  9.  
  10.         for (int start = 0; start < array.Length; start++)
  11.         {
  12.             int[] result = new int[array.Length];
  13.             int resIndex = 1;
  14.             int resLength = 1;
  15.             result[0] = array[start];
  16.             for (int i = start + 1; i < array.Length; i++)             {                 if (array[i] > result[resIndex - 1])
  17.                 {
  18.                     result[resIndex] = array[i];
  19.                     resIndex++;
  20.                     resLength++;
  21.                 }
  22.                 else if (resIndex > 1 && array[i] > result[resIndex - 2] && array[i] < result[resIndex - 1])
  23.                 {
  24.                     result[resIndex - 1] = array[i];
  25.                 }
  26.             }
  27.             if (bestLength < resLength)
  28.             {
  29.                 bestLength = resLength;
  30.                 bestResult = result;
  31.             }
  32.         }
  33.         for (int j = 0; j < bestLength; j++)
  34.         {
  35.             Console.Write(bestResult[j] + " ");
  36.         }
  37.         Console.WriteLine();
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment