Venciity

[C#] BOOK Arrays - 5 Задача

Jan 6th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. /* Write a program that finds the maximal increasing sequence in an array. Example: {3, 2, 3, 4, 2, 2, 4} ---> {2, 3, 4}. */
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _5_Exercise
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Write("Enter array length: ");
  16.             int m = int.Parse(Console.ReadLine());
  17.             int[] testArray = new int[m];
  18.  
  19.             //enter array elements
  20.             for (int i = 0; i < testArray.Length; i++)
  21.             {
  22.                 Console.Write("Number {0} = ", i + 1);
  23.                 testArray[i] = int.Parse(Console.ReadLine());
  24.             }
  25.  
  26.             List<int> maximumSeqArray = new List<int>();
  27.             int start = 0;
  28.             int length = 1;
  29.             int bestStart = 0;
  30.             int bestLength = 1;
  31.  
  32.             for (int i = 0; i < testArray.Length - 1; i++)
  33.             {
  34.                 if (testArray[i] < testArray[i + 1])
  35.                 {
  36.                     length++;
  37.  
  38.                     if (length > bestLength)
  39.                     {
  40.                         bestLength = length;
  41.                         bestStart = start;
  42.                     }
  43.                 }
  44.                 else
  45.                 {
  46.                     length = 1;
  47.                     start = i + 1;
  48.                 }
  49.             }
  50.  
  51.             int NumberOfElement = 1;
  52.             Console.WriteLine();
  53.             Console.WriteLine("Maximal increasing sequence in an array : ");
  54.             for (int n = bestStart; n < bestStart + bestLength; n++)
  55.             {
  56.                 maximumSeqArray.Add(testArray[n]);
  57.                 Console.WriteLine("element {0} : {1}", NumberOfElement, testArray[n]);
  58.                 NumberOfElement++;
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment