ellapt

T7.5.MaxIncreaseSeq

Jan 15th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class MaxIncreaseSeq
  5. {
  6. static void Main()
  7. {
  8. Console.WriteLine("Find the maximal sequence of increasing elements in an array");
  9. string inputVar;
  10. uint n;
  11. List<int> incrSeq = new List<int>(); // The result sequence
  12. int initialStart = 0;
  13. int initialLength = 1;
  14. int maxStart = 0; // the start index of the max sequence of increasing elements
  15. int maxLength = 1; // the length of the max sequence of increasing elements
  16. do
  17. {
  18. Console.Write("Enter array length: ");
  19. }
  20. while (!(uint.TryParse(inputVar = Console.ReadLine(), out n)) || n == 0);
  21.  
  22. int[] arrayOfints = new int[n];
  23.  
  24. Console.WriteLine("Now enter the elements of the array:");
  25. for (int i = 0; i < n; i++)
  26. {
  27. arrayOfints[i] = int.Parse(Console.ReadLine());
  28. }
  29.  
  30. for (int i = 0; i < n - 1; i++)
  31. {
  32. if (arrayOfints[i] < arrayOfints[i + 1])
  33. {
  34. initialLength++;
  35.  
  36. if (initialLength > maxLength)
  37. {
  38. maxLength = initialLength;
  39. maxStart = initialStart;
  40. }
  41. }
  42. else
  43. {
  44. initialLength = 1;
  45. initialStart = i + 1;
  46. }
  47. }
  48.  
  49. for (int i = maxStart; i < maxStart + maxLength; i++)
  50. {
  51. incrSeq.Add(arrayOfints[i]);
  52. }
  53. if ((initialLength = incrSeq.Count) == 1)
  54. {
  55. initialLength = 0;
  56. }
  57. Console.WriteLine("The max sequence counts {0} increasing elements", initialLength);
  58. for (int i = 0; i < initialLength; i++)
  59. {
  60. Console.Write("{0} ", incrSeq[i]);
  61. }
  62. Console.WriteLine();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment