petyaminkova91

Untitled

Oct 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ArrayManipulator
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] array = Console.ReadLine().
  11. Split()
  12. .Select(int.Parse)
  13. .ToArray();
  14.  
  15. string command = Console.ReadLine();
  16.  
  17. while (command != "end")
  18. {
  19. string[] commandParts = command.Split();
  20.  
  21. if (commandParts[0] == "exchange")
  22. {
  23. GetExchangedArray(array, commandParts);
  24. }
  25. else if (commandParts[0] == "max")
  26. {
  27. switch (commandParts[1])
  28. {
  29. case "even":
  30. PrintMaxEven(array);
  31. break;
  32. case "odd":
  33. PrintMaxOdd(array);
  34. break;
  35. }
  36. }
  37. else if (commandParts[0] == "min")
  38. {
  39. switch (commandParts[1])
  40. {
  41. case "even":
  42. PrintMinEven(array);
  43. break;
  44. case "odd":
  45. PrintMinOdd(array);
  46. break;
  47. }
  48. }
  49. else if (commandParts[0] == "first")
  50. {
  51. switch (commandParts[2])
  52. {
  53. case "even":
  54. PrintFirstEvenElements(array, commandParts);
  55. break;
  56. case "odd":
  57.  
  58. break;
  59. }
  60. }
  61.  
  62.  
  63.  
  64. command = Console.ReadLine();
  65. }
  66.  
  67. Console.WriteLine($"[{String.Join(", ", array)}]");
  68. }
  69.  
  70. static void GetExchangedArray(int[] arr, string[] array)
  71. {
  72. int IndexChange = int.Parse(array[1]);
  73.  
  74. if (IndexChange >= arr.Length || IndexChange < 0)
  75. {
  76. Console.WriteLine("Invalid index");
  77. return;
  78. }
  79.  
  80. int rotations = IndexChange + 1;
  81.  
  82. for (int i = 0; i < rotations; i++)
  83. {
  84. int first = arr[0];
  85.  
  86. for (int j = 0; j < arr.Length - 1; j++)
  87. {
  88. arr[j] = arr[j + 1];
  89. }
  90.  
  91. arr[arr.Length - 1] = first;
  92. }
  93. }
  94.  
  95. static void PrintMaxEven(int[] arr)
  96. {
  97. int maxElement = int.MinValue;
  98. int maxIndex = arr.Length;
  99.  
  100. for (int i = 0; i < arr.Length; i++)
  101. {
  102. if (arr[i] % 2 == 0 && arr[i] >= maxElement)
  103. {
  104. maxElement = arr[i];
  105. maxIndex = i;
  106. }
  107. }
  108.  
  109. if (maxIndex == arr.Length)
  110. {
  111. Console.WriteLine("No matches");
  112. }
  113. else
  114. {
  115. Console.WriteLine(maxIndex);
  116. }
  117. }
  118. static void PrintMaxOdd(int[] arr)
  119. {
  120. int maxElement = int.MinValue;
  121. int maxIndex = arr.Length;
  122.  
  123. for (int i = 0; i < arr.Length; i++)
  124. {
  125. if (arr[i] % 2 != 0 && arr[i] >= maxElement)
  126. {
  127. maxElement = arr[i];
  128. maxIndex = i;
  129. }
  130. }
  131.  
  132. if (maxIndex == arr.Length)
  133. {
  134. Console.WriteLine("No matches");
  135. }
  136. else
  137. {
  138. Console.WriteLine(maxIndex);
  139. }
  140. }
  141.  
  142. static void PrintMinEven(int[] arr)
  143. {
  144. int minElement = int.MaxValue;
  145. int minIndex = -1;
  146.  
  147. for (int i = 0; i < arr.Length; i++)
  148. {
  149. if (arr[i] % 2 == 0 && arr[i] <= minElement)
  150. {
  151. minElement = arr[i];
  152. minIndex = i;
  153. }
  154. }
  155. if (minIndex == -1)
  156. {
  157. Console.WriteLine("No matches");
  158. }
  159. else
  160. {
  161. Console.WriteLine(minIndex);
  162. }
  163. }
  164.  
  165. static void PrintMinOdd(int[] arr)
  166. {
  167. int minElement = int.MaxValue;
  168. int minIndex = -1;
  169.  
  170. for (int i = 0; i < arr.Length; i++)
  171. {
  172. if (arr[i] % 2 != 0 && arr[i] <= minElement)
  173. {
  174. minElement = arr[i];
  175. minIndex = i;
  176. }
  177. }
  178. if (minIndex == -1)
  179. {
  180. Console.WriteLine("No matches");
  181. }
  182. else
  183. {
  184. Console.WriteLine(minIndex);
  185. }
  186. }
  187.  
  188. static void PrintFirstEvenElements(int[] arr, string[] array)
  189. {
  190. int wantedCountOfEvenElements = int.Parse(array[1]);
  191.  
  192. if (wantedCountOfEvenElements > arr.Length)
  193. {
  194. Console.WriteLine("Invalid count");
  195. return;
  196. }
  197.  
  198. int[] firstEvenElements = new int[wantedCountOfEvenElements];
  199. int count = 0;
  200.  
  201. for (int i = 0; i < arr.Length; i++)
  202. {
  203. if (arr[i] % 2 == 0 && count < firstEvenElements.Length)
  204. {
  205. firstEvenElements[count] = arr[i];
  206. count++;
  207. }
  208. }
  209.  
  210. Console.WriteLine($"[{String.Join(", ", firstEvenElements)}]");
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment