Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 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. class JoinLists
  8. {
  9. static void Main()
  10. {
  11. Console.WriteLine("Please a sequence of integer numbers...");
  12. List<int> mainList = ExtractListFromConsole();
  13.  
  14. List<List<int>> difNumberSequences = new List<List<int>>();
  15. List<List<int>> sameNumberSequences = new List<List<int>>();
  16.  
  17.  
  18. for (int i = 0; i < mainList.Count; i++)
  19. {
  20.  
  21. //same number sequence
  22.  
  23. sameNumberSequences.Add(new List<int>());
  24. sameNumberSequences[sameNumberSequences.Count - 1].Add(mainList[i]);
  25.  
  26. for (int j = i + 1; j < mainList.Count; j++)
  27. {
  28. if (mainList[j]==mainList[i])
  29. {
  30. sameNumberSequences[sameNumberSequences.Count - 1].Add(mainList[i]);
  31. }
  32.  
  33. else
  34. {
  35. break;
  36. }
  37. }
  38.  
  39. //different number sequence
  40.  
  41. difNumberSequences.Add(new List<int>());
  42. difNumberSequences[difNumberSequences.Count - 1].Add(mainList[i]);
  43.  
  44. for (int j = i+1; j < mainList.Count; j++)
  45. {
  46. if (mainList[j]>mainList[i])
  47. {
  48. int counter = difNumberSequences.Count;
  49.  
  50. for (int k = 0; k < counter; k++)
  51. {
  52. if (difNumberSequences[k][difNumberSequences[k].Count - 1] == mainList[i])
  53. {
  54. difNumberSequences.Add(new List<int>(difNumberSequences[k]));
  55. difNumberSequences[k].Add(mainList[j]);
  56. }
  57. }
  58. }
  59. }
  60. difNumberSequences.RemoveAt(difNumberSequences.Count - 1);
  61. }
  62.  
  63. int maxDifNumberSequence = int.MinValue;
  64. int indexDifNumberSequence = int.MinValue;
  65.  
  66.  
  67. for (int i = 0; i < difNumberSequences.Count; i++)
  68. {
  69. if (difNumberSequences[i].Count>maxDifNumberSequence)
  70. {
  71. maxDifNumberSequence = difNumberSequences[i].Count;
  72. indexDifNumberSequence = i;
  73. }
  74. }
  75.  
  76. int maxSameNumberSequence = int.MinValue;
  77. int indexSameNumberSequence = int.MinValue;
  78.  
  79. for (int i = 0; i < sameNumberSequences.Count; i++)
  80. {
  81. if (sameNumberSequences[i].Count > maxSameNumberSequence)
  82. {
  83. maxSameNumberSequence = sameNumberSequences[i].Count;
  84. indexSameNumberSequence = i;
  85. }
  86. }
  87.  
  88. Console.WriteLine("The longest non-decreasing sequence is: ");
  89. if (maxDifNumberSequence>=maxSameNumberSequence)
  90. {
  91. PrintListInSingleLine(difNumberSequences[indexDifNumberSequence]);
  92. }
  93. else
  94. {
  95. PrintListInSingleLine(sameNumberSequences[indexSameNumberSequence]);
  96. }
  97.  
  98.  
  99. }
  100.  
  101.  
  102. private static void PrintListInSingleLine(List<int> list3)
  103. {
  104. foreach (int item in list3)
  105. {
  106. Console.Write(item + " ");
  107. }
  108. Console.WriteLine();
  109. }
  110.  
  111. private static List<int> ExtractListFromConsole()
  112. {
  113. string input = Console.ReadLine();
  114. input = input.TrimEnd();
  115. input = input.TrimStart();
  116.  
  117. string[] array1 = input.Split(' ');
  118. List<int> list1 = new List<int>();
  119.  
  120. foreach (string a in array1)
  121. {
  122. list1.Add(Convert.ToInt32(a));
  123. }
  124. return list1;
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement