Advertisement
Mhristov

arrays 06

Dec 12th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 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.  
  8. class Program
  9. {
  10.     static void Main(string[] args)
  11.     {
  12.         Console.WriteLine("Please enter an integer for the length of the array: ");
  13.         int n = int.Parse(Console.ReadLine());
  14.         int[] array = new int[n];
  15.        
  16.         List<int> sequence = new List<int>();
  17.         List<int> sequenceA = new List<int>();
  18.         Console.WriteLine("Please enter integers to fill in the array: ");
  19.         for (int i = 0; i < array.Length; i++)
  20.         {
  21.             array[i] = int.Parse(Console.ReadLine());
  22.         }
  23.  
  24.         for (int i = 0; i < array.Length - 1; i++)
  25.         {
  26.             if(array[i]<array[i+1])
  27.             {
  28.                 sequence.Add(array[i]);
  29.                 if((i<=array.Length-3&&array[i+2]<=array[i+1])||(i==array.Length-2&&array[i]<array[i+1]))
  30.                 {
  31.                     sequence.Add(array[i + 1]);
  32.                 }
  33.             }
  34.             if(sequenceA.Count<sequence.Count)
  35.             {
  36.                 sequenceA = sequence.ToList();
  37.             }
  38.             else if (array[i]>=array[i+1])
  39.             {
  40.                 sequence.Clear();
  41.             }
  42.         }
  43.  
  44.         Console.WriteLine("The longest increasing sequence is: ");
  45.         foreach(int member in sequenceA)
  46.         {
  47.             Console.WriteLine(member+" ");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement