Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //максималната поредица от нарастващи елементи в масив arr[n]. Елементите може и да не са последователни.
- static void Main(string[] args)
- {
- int[] array = new int[] { 6, 9, 2, 3, 8, 7, 6, 5, 8, 4 };//==> 2, 3, 5, 8
- int bestLength = 0;
- int[] bestResult = new int[array.Length];
- for (int start = 0; start < array.Length; start++)
- {
- int[] result = new int[array.Length];
- int resIndex = 1;
- int resLength = 1;
- result[0] = array[start];
- for (int i = start + 1; i < array.Length; i++) { if (array[i] > result[resIndex - 1])
- {
- result[resIndex] = array[i];
- resIndex++;
- resLength++;
- }
- else if (resIndex > 1 && array[i] > result[resIndex - 2] && array[i] < result[resIndex - 1])
- {
- result[resIndex - 1] = array[i];
- }
- }
- if (bestLength < resLength)
- {
- bestLength = resLength;
- bestResult = result;
- }
- }
- for (int j = 0; j < bestLength; j++)
- {
- Console.Write(bestResult[j] + " ");
- }
- Console.WriteLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment