Advertisement
GabrielDas

Untitled

Jun 14th, 2019
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace zad_11
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11.  
  12. int[] arr = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  13.  
  14.  
  15. while (true)
  16. {
  17.  
  18. string[] command = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
  19.  
  20. if (command[0] == "end")
  21. {
  22. break;
  23.  
  24. }
  25. if (command[0] == "exchange")
  26. {
  27. int index = int.Parse(command[1]);
  28.  
  29. arr = Exchange(arr, index);
  30.  
  31. }
  32. else if (command[0] == "max")
  33. {
  34. string type = command[1];
  35.  
  36. Max(arr, type);
  37.  
  38.  
  39. }
  40. else if (command[0] == "min")
  41. {
  42. string type = command[1];
  43.  
  44. Min(arr, type);
  45.  
  46. }
  47. else if (command[0] == "first")
  48. {
  49. int count = int.Parse(command[1]);
  50. string type = command[2];
  51.  
  52. FirstNumbers(arr, count, type);
  53. }
  54. else if (command[0] == "last")
  55. {
  56.  
  57. int count = int.Parse(command[1]);
  58. string type = command[2];
  59.  
  60. LastNumbers(arr, count, type);
  61. }
  62.  
  63.  
  64. }
  65.  
  66. Console.WriteLine($"[{String.Join(", ", arr)}]");
  67.  
  68.  
  69.  
  70.  
  71. }
  72.  
  73. private static int[] LastNumbers(int[] arr, int count, string type)
  74. {
  75. if(count > arr.Length)
  76. {
  77. Console.WriteLine("Invalid count");
  78. return arr;
  79.  
  80. }
  81.  
  82. if(type == "odd")
  83. {
  84. var listOdd = new List<int>();
  85.  
  86. for (int i = arr.Length-1; i >= 0; i--)
  87. {
  88. if(arr[i] % 2 != 0)
  89. {
  90. if(count == 0)
  91. {
  92. break;
  93. }
  94.  
  95. listOdd.Add(arr[i]);
  96. count--;
  97.  
  98. }
  99.  
  100. }
  101.  
  102. listOdd.Reverse();
  103. int[] arrOdd = listOdd.ToArray();
  104. Console.WriteLine($"[{String.Join(", ", arrOdd)}]");
  105.  
  106. return arrOdd;
  107.  
  108. }
  109. else
  110. {
  111. var listEven = new List<int>();
  112.  
  113. for (int i = arr.Length - 1; i >= 0; i--)
  114. {
  115. if(arr[i] % 2 == 0)
  116. {
  117. if(count == 0)
  118. {
  119. break;
  120. }
  121.  
  122. listEven.Add(arr[i]);
  123. count--;
  124.  
  125. }
  126.  
  127. }
  128.  
  129. listEven.Reverse();
  130. int[] arrEven = listEven.ToArray();
  131. Console.WriteLine($"[{String.Join(", ", arrEven)}]");
  132.  
  133. return arrEven;
  134.  
  135.  
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142. return arr;
  143. }
  144.  
  145. private static int[] FirstNumbers(int[] arr, int count, string type)
  146. {
  147. if(count > arr.Length)
  148. {
  149. Console.WriteLine("Invalid count");
  150. return arr;
  151. }
  152.  
  153. if(type == "odd")
  154. {
  155. var listOdd = new List<int>();
  156.  
  157. for (int i = 0; i < arr.Length; i++)
  158. {
  159. if(arr[i] % 2 != 0)
  160. {
  161. if (count == 0)
  162. {
  163. break;
  164. }
  165.  
  166. listOdd.Add(arr[i]);
  167. count--;
  168.  
  169.  
  170. }
  171.  
  172. }
  173.  
  174. var arrOdd = listOdd.ToArray();
  175. Console.WriteLine($"[{String.Join(", ", arrOdd)}]");
  176.  
  177. return arrOdd;
  178.  
  179.  
  180. }
  181. else
  182. {
  183.  
  184. var listEven = new List<int>();
  185.  
  186. for (int i = 0; i < arr.Length; i++)
  187. {
  188. if(arr[i] % 2 == 0 )
  189. {
  190. if (count == 0)
  191. {
  192. break;
  193. }
  194.  
  195. listEven.Add(arr[i]);
  196. count--;
  197.  
  198.  
  199.  
  200. }
  201.  
  202. }
  203.  
  204. var arrEven = listEven.ToArray();
  205. Console.WriteLine($"[{String.Join(", ", arrEven)}]");
  206.  
  207. return arrEven;
  208.  
  209. }
  210.  
  211.  
  212. }
  213.  
  214. private static void Min(int[] arr, string type)
  215. {
  216. int pos = -1;
  217.  
  218. if (type == "odd")
  219. {
  220. bool areThereOdd = false;
  221. int min = int.MaxValue;
  222.  
  223. for (int i = 0; i < arr.Length; i++)
  224. {
  225. if (arr[i] % 2 != 0 && arr[i] <= min)
  226. {
  227. min = arr[i];
  228. pos = i;
  229. areThereOdd = true;
  230.  
  231. }
  232.  
  233. }
  234.  
  235. if (areThereOdd == true)
  236. {
  237. Console.WriteLine(pos);
  238. }
  239. else
  240. {
  241. Console.WriteLine("No matches");
  242. }
  243.  
  244. }
  245. else
  246. {
  247. bool areThereEven = false;
  248. int min = int.MaxValue;
  249.  
  250. for (int i = 0; i < arr.Length; i++)
  251. {
  252.  
  253. if (arr[i] % 2 == 0 && arr[i] <= min)
  254. {
  255. min = arr[i];
  256. pos = i;
  257. areThereEven = true;
  258. }
  259.  
  260. }
  261.  
  262. if (areThereEven == true)
  263. {
  264. Console.WriteLine(pos);
  265. }
  266. else
  267. {
  268. Console.WriteLine("No matches");
  269. }
  270.  
  271. }
  272.  
  273.  
  274.  
  275.  
  276. }
  277.  
  278. private static void Max(int[] arr, string type)
  279. {
  280. int pos = -1;
  281. int max = int.MinValue;
  282.  
  283. if (type == "odd")
  284. {
  285. bool areThereOdd = false;
  286. for (int i = 0; i < arr.Length; i++)
  287. {
  288. if (arr[i] % 2 != 0 && arr[i] >= max)
  289. {
  290. max = arr[i];
  291. pos = i;
  292. areThereOdd = true;
  293. }
  294.  
  295. }
  296.  
  297. if (areThereOdd == true)
  298. {
  299. Console.WriteLine(pos);
  300. }
  301. else
  302. {
  303. Console.WriteLine("No matches");
  304. }
  305.  
  306. }
  307. else
  308. {
  309. bool areThereEven = false;
  310. for (int i = 0; i < arr.Length; i++)
  311. {
  312. if (arr[i] % 2 == 0 && arr[i] >= max)
  313. {
  314.  
  315. max = arr[i];
  316. pos = i;
  317. areThereEven = true;
  318. }
  319.  
  320. }
  321. if (areThereEven == true)
  322. {
  323. Console.WriteLine(pos);
  324. }
  325. else
  326. {
  327. Console.WriteLine("No matches");
  328. }
  329.  
  330.  
  331. }
  332.  
  333.  
  334. }
  335.  
  336. static private int[] Exchange(int[] arr, int index)
  337. {
  338. if (index < 0 || index > arr.Length - 1)
  339. {
  340. Console.WriteLine("Invalid index");
  341. return arr;
  342.  
  343. }
  344.  
  345.  
  346. int[] tempArr = new int[arr.Length];
  347. int counter = 0;
  348.  
  349.  
  350. for (int i = index + 1; i < arr.Length; i++)
  351. {
  352. tempArr[counter] = arr[i];
  353. counter++;
  354.  
  355. }
  356.  
  357. for (int j = 0; j <= index; j++)
  358. {
  359. tempArr[counter] = arr[j];
  360. counter++;
  361. }
  362.  
  363. arr = tempArr;
  364.  
  365. return arr;
  366. }
  367. }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement