Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class JoinLists
- {
- static void Main()
- {
- Console.WriteLine("Please a sequence of integer numbers...");
- List<int> mainList = ExtractListFromConsole();
- List<List<int>> difNumberSequences = new List<List<int>>();
- List<List<int>> sameNumberSequences = new List<List<int>>();
- for (int i = 0; i < mainList.Count; i++)
- {
- //same number sequence
- sameNumberSequences.Add(new List<int>());
- sameNumberSequences[sameNumberSequences.Count - 1].Add(mainList[i]);
- for (int j = i + 1; j < mainList.Count; j++)
- {
- if (mainList[j]==mainList[i])
- {
- sameNumberSequences[sameNumberSequences.Count - 1].Add(mainList[i]);
- }
- else
- {
- break;
- }
- }
- //different number sequence
- difNumberSequences.Add(new List<int>());
- difNumberSequences[difNumberSequences.Count - 1].Add(mainList[i]);
- for (int j = i+1; j < mainList.Count; j++)
- {
- if (mainList[j]>mainList[i])
- {
- int counter = difNumberSequences.Count;
- for (int k = 0; k < counter; k++)
- {
- if (difNumberSequences[k][difNumberSequences[k].Count - 1] == mainList[i])
- {
- difNumberSequences.Add(new List<int>(difNumberSequences[k]));
- difNumberSequences[k].Add(mainList[j]);
- }
- }
- }
- }
- difNumberSequences.RemoveAt(difNumberSequences.Count - 1);
- }
- int maxDifNumberSequence = int.MinValue;
- int indexDifNumberSequence = int.MinValue;
- for (int i = 0; i < difNumberSequences.Count; i++)
- {
- if (difNumberSequences[i].Count>maxDifNumberSequence)
- {
- maxDifNumberSequence = difNumberSequences[i].Count;
- indexDifNumberSequence = i;
- }
- }
- int maxSameNumberSequence = int.MinValue;
- int indexSameNumberSequence = int.MinValue;
- for (int i = 0; i < sameNumberSequences.Count; i++)
- {
- if (sameNumberSequences[i].Count > maxSameNumberSequence)
- {
- maxSameNumberSequence = sameNumberSequences[i].Count;
- indexSameNumberSequence = i;
- }
- }
- Console.WriteLine("The longest non-decreasing sequence is: ");
- if (maxDifNumberSequence>=maxSameNumberSequence)
- {
- PrintListInSingleLine(difNumberSequences[indexDifNumberSequence]);
- }
- else
- {
- PrintListInSingleLine(sameNumberSequences[indexSameNumberSequence]);
- }
- }
- private static void PrintListInSingleLine(List<int> list3)
- {
- foreach (int item in list3)
- {
- Console.Write(item + " ");
- }
- Console.WriteLine();
- }
- private static List<int> ExtractListFromConsole()
- {
- string input = Console.ReadLine();
- input = input.TrimEnd();
- input = input.TrimStart();
- string[] array1 = input.Split(' ');
- List<int> list1 = new List<int>();
- foreach (string a in array1)
- {
- list1.Add(Convert.ToInt32(a));
- }
- return list1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement