Advertisement
Guest User

Untitled

a guest
Feb 13th, 2020
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _11._Array_Manipulator
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray(); //it works
  12. string input = Console.ReadLine();
  13. //int[] tempArray = new int[array.Length];
  14.  
  15. while (input != "end")
  16. {
  17. string[] command = input.Split().ToArray();
  18. if (command[0] == "exchange") // it works
  19. {
  20. //int delimer = int.Parse(command[1].ToString()); //тук не ти трябва ToString!?
  21. int delimer = int.Parse(command[1]);
  22. if (delimer < 0 || delimer > array.Length - 1)
  23. {
  24. Console.WriteLine("Invalid index");
  25. }
  26. else
  27. {
  28. array = Exchange(array, delimer);
  29. }
  30. }
  31. else if (command[0] == "max")
  32. {
  33. string evenOrOdd = command[1].ToString();
  34. if (evenOrOdd == "even")
  35. {
  36. int maxIndex = MaxEvenIndex(array, command);//това го вадя от иф проверката
  37.  
  38. if (maxIndex != -1) //
  39. {
  40. Console.WriteLine(maxIndex);
  41. }
  42. else //ако върне -1 като резултат влизаме в елса
  43. {
  44. Console.WriteLine("No matches");
  45. }
  46. } // ДО ТУК ДОБРЕ
  47. else if (evenOrOdd == "odd")
  48. {
  49. int maxIndex = MaxOddIndex(array, command);
  50. if (maxIndex != -1)
  51. {
  52. Console.WriteLine(maxIndex);
  53. }
  54. else
  55. {
  56. Console.WriteLine("No matches");
  57. }
  58. }
  59. }
  60. else if (command[0] == "min")
  61. {
  62. string evenOrOddMin = command[1]; //it works
  63. if (evenOrOddMin == "even")
  64. {
  65. int minIndex = MinEvenIndex(array, command);
  66.  
  67. if (minIndex != -1)
  68. {
  69. Console.WriteLine(minIndex);
  70. }
  71. else
  72. {
  73. Console.WriteLine("No matches");
  74. }
  75. }
  76. else if (evenOrOddMin == "odd")
  77. {
  78. int minIndex = MinOddIndex(array, command);
  79.  
  80. if (minIndex != -1)
  81. {
  82. Console.WriteLine(minIndex);
  83. }
  84. else
  85. {
  86. Console.WriteLine("No matches");
  87. }
  88. }
  89. } // ДО ТУК РАБОТИ
  90. else if (command[0] == "first")
  91. {
  92. int counter = int.Parse(command[1].ToString());
  93. if (counter > array.Length)
  94. {
  95. Console.WriteLine("Invalid count");
  96. }
  97. else
  98. {
  99. if (command[2] == "even")
  100. {
  101. if (IsEvenInArray(array) == false)
  102. {
  103. Console.WriteLine($"[]");
  104. }
  105. else
  106. {
  107. FirstEvenElements(array, counter);
  108. }
  109. }
  110. else if (command[2] == "odd")
  111. {
  112. if (IsOddInArray(array) == false)
  113. {
  114. Console.WriteLine($"[]");
  115. }
  116. else
  117. {
  118. FirstOddElements(array, counter);
  119. }
  120. }
  121. }
  122. }
  123. else if (command[0] == "last")
  124. {
  125. int counter = int.Parse(command[1].ToString());
  126. if (counter > array.Length)
  127. {
  128. Console.WriteLine("Invalid count");
  129. }
  130. else
  131. {
  132. if (command[2] == "even")
  133. {
  134. if (IsEvenInArray(array) == false)
  135. {
  136. Console.WriteLine($"[]");
  137. }
  138. else
  139. {
  140. LastEvenElements(array, counter);
  141. }
  142. }
  143. else if (command[2] == "odd")
  144. {
  145. if (IsOddInArray(array) == false)
  146. {
  147. Console.WriteLine($"[]");
  148. }
  149. else
  150. {
  151. LastOddElements(array, counter);
  152. }
  153. }
  154. }
  155. }
  156. input = Console.ReadLine();
  157. }
  158. Console.WriteLine($"[{string.Join(", ", array)}]");
  159. }
  160.  
  161. static int[] Exchange(int[] array, int delimer)
  162. {
  163. array = array.Skip(delimer + 1).Concat(array.Take(delimer + 1)).ToArray();
  164. return array;
  165. }
  166. static bool IsEvenInArray(int[] array)
  167. {
  168. bool isEvenInArray = false;
  169.  
  170. for (int i = 0; i < array.Length; i++)
  171. {
  172. if (array[i] % 2 == 0)
  173. {
  174. isEvenInArray = true;
  175. }
  176. }
  177. return isEvenInArray;
  178. }
  179. static bool IsOddInArray(int[] array)
  180. {
  181. bool isOddInArray = false;
  182.  
  183. for (int i = 0; i < array.Length; i++)
  184. {
  185. if (array[i] % 2 != 0)
  186. {
  187. isOddInArray = true;
  188. }
  189. }
  190. return isOddInArray;
  191. }
  192. static int MaxEvenIndex(int[] array, string[] command)
  193. {
  194.  
  195. int maxEvenNumber = int.MinValue;
  196. int maxEvenNumberIndex = -1; // ако търсим индекси никога не сетваме променлива на "0", защото нула е валиден индекс
  197.  
  198. for (int i = 0; i < array.Length; i++)
  199. {
  200. if (array[i] % 2 == 0)
  201. {
  202. //if (array[i] > maxEvenNumber) If there are two or more equal min/max elements, return the index of the rightmost one
  203. if (array[i] >= maxEvenNumber)
  204. {
  205. maxEvenNumber = array[i]; //тук търсим максималният елемент
  206. //if (i > maxEvenNumberIndex)
  207. //{
  208. // maxEvenNumberIndex = i; // това цялото е излишно
  209. //}
  210. }
  211. }
  212.  
  213. }
  214. return maxEvenNumberIndex = array.ToList().LastIndexOf(maxEvenNumber); ; // връщаме индекса на максималният елемент + използваме ToList за да можем да извикаме функцията LastIndexOf
  215. }
  216. static int MaxOddIndex(int[] array, string[] command)
  217. {
  218. int maxOddNumber = int.MinValue;
  219. int maxNumberIndex = -1;
  220.  
  221. for (int i = 0; i < array.Length; i++)
  222. {
  223. if (array[i] % 2 != 0)
  224. {
  225. //if (array[i] > maxOddNumber) If there are two or more equal min/max elements, return the index of the rightmost one
  226. if (array[i] >= maxOddNumber)
  227. {
  228. maxOddNumber = array[i];
  229. }
  230. }
  231. }
  232. return maxNumberIndex = array.ToList().LastIndexOf(maxOddNumber);
  233. }
  234. static int MinOddIndex(int[] array, string[] command)
  235. {
  236. int minOddNumber = int.MaxValue;
  237. int minNumberIndex = -1;
  238.  
  239. for (int i = 0; i < array.Length; i++)
  240. {
  241. if (array[i] % 2 != 0)
  242. {
  243. //if (array[i] < minOddNumber)
  244. if (array[i] <= minOddNumber) //If there are two or more equal min/max elements, return the index of the rightmost one
  245. {
  246. minOddNumber = array[i];
  247. }
  248. }
  249. }
  250. return minNumberIndex = array.ToList().LastIndexOf(minOddNumber);
  251. }
  252. static int MinEvenIndex(int[] array, string[] command)
  253. {
  254. int minEvenNumber = int.MaxValue;
  255. int minNumberIndex = -1;
  256.  
  257. for (int i = 0; i < array.Length; i++)
  258. {
  259. if (array[i] % 2 == 0)
  260. {
  261. //if (array[i] < minEvenNumber) //If there are two or more equal min/max elements, return the index of the rightmost one
  262. if (array[i] <= minEvenNumber)
  263. {
  264. minEvenNumber = array[i];
  265.  
  266. }
  267. }
  268. }
  269. return minNumberIndex = array.ToList().LastIndexOf(minEvenNumber);
  270. }
  271. static void FirstEvenElements(int[] array, int counter)
  272. {
  273. string[] result = new string[array.Length];
  274. int currentCount = 0;
  275.  
  276. for (int i = 0; i < array.Length; i++)
  277. {
  278. if (array[i] % 2 == 0 && counter > currentCount)
  279. {
  280. currentCount++;
  281. result[i] = array[i].ToString();
  282. }
  283. }
  284.  
  285. result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  286. Console.WriteLine($"[{string.Join(", ", result)}]");
  287. }
  288. static void FirstOddElements(int[] array, int counter)
  289. {
  290. string[] result = new string[array.Length];
  291. int currentCount = 0;
  292.  
  293. for (int i = 0; i < array.Length; i++)
  294. {
  295. if (array[i] % 2 != 0 && counter > currentCount)
  296. {
  297. currentCount++;
  298. result[i] = array[i].ToString();
  299. }
  300. }
  301.  
  302. result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  303. Console.WriteLine($"[{string.Join(", ", result)}]");
  304. }
  305. static void LastEvenElements(int[] array, int counter)
  306. {
  307. //array = array.Reverse().ToArray(); - тук не е добра идея да си обръщаш основната колекция - по добре ако искаш да я ползваш обърната направи друга променлива която да сочи към нея и работи с втората променлива
  308. List<int> result = new List<int>();
  309. int currentCount = 0;
  310.  
  311. for (int i = array.Length -1; i >= 0; i--)
  312. {
  313. if (array[i] % 2 == 0 && counter > currentCount)
  314. {
  315. currentCount++;
  316. result.Add(array[i]);
  317. }
  318. }
  319.  
  320. result.Reverse();// това е най кофти ситуацията от цялата задача - ако не обърна тази колекция ще ми ги отпечата в обратен ред(в реда на добавяне), което на judge не му харесва.
  321. //result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  322. Console.WriteLine($"[{string.Join(", ", result)}]");
  323. }
  324. static void LastOddElements(int[] array, int counter)
  325. {
  326. //array = array.Reverse().ToArray();
  327. //string[] result = new string[array.Length]; // инт масив , не стринг, за какво ти е стринг като работим с числа
  328. List<int> result = new List<int>();
  329. int currentCount = 0;
  330.  
  331. for (int i = array.Length -1; i >= 0; i--)
  332. {
  333. if (array[i] % 2 != 0 && counter > currentCount)
  334. {
  335. currentCount++;
  336. result.Add(array[i]);
  337. }
  338. }
  339.  
  340. result.Reverse();
  341.  
  342. Console.WriteLine($"[{string.Join(", ", result)}]");
  343. }
  344. }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement