Advertisement
Guest User

Untitled

a guest
Sep 17th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem_05
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int[] inputElements = Console.ReadLine()
  12.                 .Split(' ')
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.             int arrLength = inputElements.Length;
  16.             int longestSequence = 1;
  17.             int testSequenceLength = 1;
  18.             int longestSequenceEndPos = 0;
  19.             List<int> longestSeqReversed = new List<int>();
  20.  
  21.             for (int i = 0; i < arrLength; i++) // The purpose of this loop is to print the sequences on separate lines and to record the end position of the longest sequence, and its length
  22.             {
  23.                 Console.Write(inputElements[i]);
  24.                 if (i < inputElements.Length - 1 && (inputElements[i] < inputElements[i + 1]))
  25.                 {
  26.                     Console.Write(" ");
  27.                     testSequenceLength++;
  28.                 }
  29.                 else
  30.                 {
  31.                     Console.WriteLine();
  32.                     if (testSequenceLength > longestSequence)
  33.                     {
  34.                         longestSequence = testSequenceLength;
  35.                         longestSequenceEndPos = i;
  36.                     }
  37.                     testSequenceLength = 1;
  38.                 }
  39.             }
  40.             Console.Write("Longest: ");
  41.             // Using the data we acquired with the previous loop, we now start from the end of the longest sequence and record its numbers in a list, in a reversed order
  42.             for (int i = longestSequenceEndPos; i > longestSequenceEndPos - longestSequence; i--)
  43.             {
  44.                 if (i < 0)
  45.                 {
  46.                     break;
  47.                 }
  48.                 longestSeqReversed.Add(inputElements[i]);
  49.                
  50.             }
  51.             // We print the list in reversed order, to show the longest sequence in ascending order
  52.             for (int i = longestSeqReversed.Count-1; i >= 0; i--)
  53.             {
  54.                 Console.Write(longestSeqReversed[i]+" ");
  55.             }
  56.            
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement