stefan1919

Longest increasing sequence

Sep 9th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication6
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] input = Console.ReadLine().Split(' ');
  14.             List<int> output = new List<int>();
  15.             List<int> arrayOutput = new List<int>();
  16.             int[] array = new int[input.Length];
  17.             for (int i = 0; i < array.Length; i++)
  18.             {
  19.                 array[i] = int.Parse(input[i]);
  20.             }
  21.             int previousCounter = 0;
  22.             int counter = 0;
  23.             for (int i = 0; i < array.Length - 1; i++)
  24.             {
  25.                 if (array[i] < array[i + 1])
  26.                 {
  27.                     output.Add(array[i]);
  28.                     counter++;
  29.                 }
  30.                 else
  31.                 {
  32.                     output.Add(array[i]);
  33.                     counter++;
  34.                     if (counter > previousCounter)
  35.                     {
  36.                         arrayOutput.Clear();
  37.                         foreach (var item in output)
  38.                         {
  39.                             arrayOutput.Add(item);
  40.                         }
  41.                         previousCounter = counter;
  42.                     }
  43.                     foreach (var item in output)
  44.                     {
  45.                         Console.Write(item + " ");
  46.                        
  47.                     }
  48.                     Console.WriteLine();
  49.                     output.Clear();
  50.                     counter = 0;
  51.                 }
  52.                 if (i == array.Length - 2)
  53.                 {
  54.                     if (array[i] < array[i + 1])
  55.                     {
  56.                         output.Add(array[i + 1]);
  57.                         counter++;
  58.                         if (counter > previousCounter)
  59.                         {
  60.                             arrayOutput.Clear();
  61.                             foreach (var item in output)
  62.                             {
  63.                                 arrayOutput.Add(item);
  64.                             }
  65.                             previousCounter = counter;
  66.                         }
  67.                         foreach (var item in output)
  68.                         {
  69.                             Console.Write(item + " ");
  70.                         }
  71.                         Console.WriteLine();
  72.                     }
  73.                     else
  74.                     {
  75.                         foreach (var item in output)
  76.                         {
  77.                             Console.Write(item + " ");
  78.                         }
  79.                         Console.WriteLine(array[i + 1]);
  80.                     }
  81.                 }
  82.             }
  83.             Console.WriteLine("Longest:{0}", string.Join(" ", arrayOutput));
  84.             }
  85.         }
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment