Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to find all increasing sequences inside an array of integers.
- * The integers are given on a single line, separated by a space.
- * Print the sequences in the order of their appearance in the input array,
- * each at a single line. Separate the sequence elements by a space.
- * Find also the longest increasing sequence and print it at the last line.
- * If several sequences have the same longest length, print the left-most of them.
- */
- namespace _05.LongestIncreasingSequence
- {
- using System;
- using System.Linq;
- class LongestIncreasingSequence
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter your Integer numbers on single line, separated by a space: ");
- int[] numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- int longStart = 0;
- int start = 0;
- int longEnd = 0;
- int longLength = 0;
- int length = 0;
- for (int i = 0; i < numbers.Length - 1; i++)
- {
- Console.Write(numbers[i]);
- if (numbers[i] < numbers[i + 1])
- {
- Console.Write(" ");
- length++;
- }
- else
- {
- Console.WriteLine();
- if (length > longLength)
- {
- longLength = length;
- longStart = start;
- longEnd = i;
- }
- length = 0;
- start = i + 1;
- }
- }
- Console.WriteLine(numbers[numbers.Length - 1]);
- if (length > longLength)
- {
- longLength = length;
- longStart = start;
- longEnd = numbers.Length - 1;
- }
- Console.Write("Longest:");
- for (int i = longStart; i <= longEnd; i++)
- {
- Console.Write(" {0}", numbers[i]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement