Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace zad_11
- {
- class Program
- {
- static void Main()
- {
- int[] arr = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- while (true)
- {
- string[] command = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
- if (command[0] == "end")
- {
- break;
- }
- if (command[0] == "exchange")
- {
- int index = int.Parse(command[1]);
- arr = Exchange(arr, index);
- }
- else if (command[0] == "max")
- {
- string type = command[1];
- Max(arr, type);
- }
- else if (command[0] == "min")
- {
- string type = command[1];
- Min(arr, type);
- }
- else if (command[0] == "first")
- {
- int count = int.Parse(command[1]);
- string type = command[2];
- FirstNumbers(arr, count, type);
- }
- else if (command[0] == "last")
- {
- int count = int.Parse(command[1]);
- string type = command[2];
- LastNumbers(arr, count, type);
- }
- }
- Console.WriteLine($"[{String.Join(", ", arr)}]");
- }
- private static int[] LastNumbers(int[] arr, int count, string type)
- {
- if(count > arr.Length)
- {
- Console.WriteLine("Invalid count");
- return arr;
- }
- if(type == "odd")
- {
- var listOdd = new List<int>();
- for (int i = arr.Length-1; i >= 0; i--)
- {
- if(arr[i] % 2 != 0)
- {
- if(count == 0)
- {
- break;
- }
- listOdd.Add(arr[i]);
- count--;
- }
- }
- listOdd.Reverse();
- int[] arrOdd = listOdd.ToArray();
- Console.WriteLine($"[{String.Join(", ", arrOdd)}]");
- return arrOdd;
- }
- else
- {
- var listEven = new List<int>();
- for (int i = arr.Length - 1; i >= 0; i--)
- {
- if(arr[i] % 2 == 0)
- {
- if(count == 0)
- {
- break;
- }
- listEven.Add(arr[i]);
- count--;
- }
- }
- listEven.Reverse();
- int[] arrEven = listEven.ToArray();
- Console.WriteLine($"[{String.Join(", ", arrEven)}]");
- return arrEven;
- }
- return arr;
- }
- private static int[] FirstNumbers(int[] arr, int count, string type)
- {
- if(count > arr.Length)
- {
- Console.WriteLine("Invalid count");
- return arr;
- }
- if(type == "odd")
- {
- var listOdd = new List<int>();
- for (int i = 0; i < arr.Length; i++)
- {
- if(arr[i] % 2 != 0)
- {
- if (count == 0)
- {
- break;
- }
- listOdd.Add(arr[i]);
- count--;
- }
- }
- var arrOdd = listOdd.ToArray();
- Console.WriteLine($"[{String.Join(", ", arrOdd)}]");
- return arrOdd;
- }
- else
- {
- var listEven = new List<int>();
- for (int i = 0; i < arr.Length; i++)
- {
- if(arr[i] % 2 == 0 )
- {
- if (count == 0)
- {
- break;
- }
- listEven.Add(arr[i]);
- count--;
- }
- }
- var arrEven = listEven.ToArray();
- Console.WriteLine($"[{String.Join(", ", arrEven)}]");
- return arrEven;
- }
- }
- private static void Min(int[] arr, string type)
- {
- int pos = -1;
- if (type == "odd")
- {
- bool areThereOdd = false;
- int min = int.MaxValue;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] % 2 != 0 && arr[i] <= min)
- {
- min = arr[i];
- pos = i;
- areThereOdd = true;
- }
- }
- if (areThereOdd == true)
- {
- Console.WriteLine(pos);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else
- {
- bool areThereEven = false;
- int min = int.MaxValue;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] % 2 == 0 && arr[i] <= min)
- {
- min = arr[i];
- pos = i;
- areThereEven = true;
- }
- }
- if (areThereEven == true)
- {
- Console.WriteLine(pos);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- private static void Max(int[] arr, string type)
- {
- int pos = -1;
- int max = int.MinValue;
- if (type == "odd")
- {
- bool areThereOdd = false;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] % 2 != 0 && arr[i] >= max)
- {
- max = arr[i];
- pos = i;
- areThereOdd = true;
- }
- }
- if (areThereOdd == true)
- {
- Console.WriteLine(pos);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else
- {
- bool areThereEven = false;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] % 2 == 0 && arr[i] >= max)
- {
- max = arr[i];
- pos = i;
- areThereEven = true;
- }
- }
- if (areThereEven == true)
- {
- Console.WriteLine(pos);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- static private int[] Exchange(int[] arr, int index)
- {
- if (index < 0 || index > arr.Length - 1)
- {
- Console.WriteLine("Invalid index");
- return arr;
- }
- int[] tempArr = new int[arr.Length];
- int counter = 0;
- for (int i = index + 1; i < arr.Length; i++)
- {
- tempArr[counter] = arr[i];
- counter++;
- }
- for (int j = 0; j <= index; j++)
- {
- tempArr[counter] = arr[j];
- counter++;
- }
- arr = tempArr;
- return arr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement