Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace ArrayManipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] mainArray = Console.ReadLine().Split().Select(int.Parse).ToArray();
- while (true)
- {
- string[] input = Console.ReadLine().Split().ToArray();
- if (input[0] == "end")
- {
- break;
- }
- if (input[0] == "exchange")
- {
- int exchangeIndex = int.Parse(input[1]);
- if (exchangeIndex > mainArray.Length - 1 && exchangeIndex < 0)
- {
- Console.WriteLine("Invalid index");
- continue;
- }
- Exchange(mainArray, exchangeIndex);
- }
- else if (input[0] == "max")
- {
- int index = -1;
- if (input[1] == "even")
- {
- index = GetMaxEvenOrOddIndex(mainArray, 0);
- }
- else if (input[1] == "odd")
- {
- index = GetMaxEvenOrOddIndex(mainArray, 1);
- }
- if (index == -1)
- {
- Console.WriteLine("No matches");
- continue;
- }
- Console.WriteLine(index);
- }
- else if (input[0] == "min")
- {
- int index = -1;
- if (input[1] == "even")
- {
- index = GetMinEvenOrOddIndex(mainArray, 0);
- }
- else if (input[1] == "odd")
- {
- index = GetMinEvenOrOddIndex(mainArray, 1);
- }
- if (index == -1)
- {
- Console.WriteLine("No matches");
- continue;
- }
- Console.WriteLine(index);
- }
- else if (input[0] == "first")
- {
- int[] result = new int[0];
- int count = int.Parse(input[1]);
- if (count > mainArray.Length)
- {
- Console.WriteLine("Invalid count");
- continue;
- }
- if (input[2] == "even")
- {
- result = GetFirstEvenOrOddNumber(mainArray, count, 0);
- }
- else if (input[2] == "odd")
- {
- result = GetFirstEvenOrOddNumber(mainArray, count, 1);
- }
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- else if (input[0] == "last")
- {
- int[] result = new int[0];
- int count = int.Parse(input[1]);
- if (count>mainArray.Length)
- {
- Console.WriteLine("Invalid count");
- continue;
- }
- if (input[2] == "even")
- {
- result = GetLastEvenOrOddNumber(mainArray, count, 0);
- }
- else if (input[2] == "odd")
- {
- result = GetLastEvenOrOddNumber(mainArray, count, 1);
- }
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- }
- Console.WriteLine($"[{string.Join(", ",mainArray)}]");
- }
- private static int[] GetLastEvenOrOddNumber(int[] mainArray, int count, int divisionResult)
- {
- int[] arrResult = new int[count];
- int curentCount = 0;
- for (int i = mainArray.Length-1; i >= 0; i--)
- {
- if (mainArray[i] % 2 == divisionResult && curentCount < count)
- {
- arrResult[curentCount] = mainArray[i];
- curentCount++;
- }
- }
- if (curentCount >= count)
- {
- return arrResult.Reverse().ToArray();
- }
- else
- {
- int[] temp = new int[curentCount];
- Array.Copy(arrResult, temp, curentCount);
- return temp.Reverse().ToArray();
- }
- }
- private static int[] GetFirstEvenOrOddNumber(int[] mainArray, int count, int divisionResult)
- {
- int[] arrResult = new int[count];
- int curentCount = 0;
- for (int i = 0; i < mainArray.Length; i++)
- {
- if (mainArray[i] % 2 == divisionResult && curentCount < count)
- {
- arrResult[curentCount] = mainArray[i];
- curentCount++;
- }
- }
- if (curentCount >= count)
- {
- return arrResult;
- }
- else
- {
- int[] temp = new int[curentCount];
- Array.Copy(arrResult,temp,curentCount);
- return temp;
- }
- }
- private static int GetMinEvenOrOddIndex(int[] array, int divisionResult)
- {
- int minNumber = int.MaxValue;
- int index = -1;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] < minNumber && array[i] % 2 == divisionResult)
- {
- minNumber = array[i];
- index = i;
- }
- }
- return index;
- }
- private static int GetMaxEvenOrOddIndex(int[] array, int divisionResult)
- {
- int maxNumber = int.MinValue;
- int index = -1;
- for (int i = 0; i <array.Length; i++)
- {
- if (array[i] >= maxNumber && array[i] % 2 == divisionResult)
- {
- maxNumber = array[i];
- index = i;
- }
- }
- return index;
- }
- private static void Exchange(int[] mainArray, int exchangeIndex)
- {
- for (int i = 0; i < exchangeIndex + 1; i++)
- {
- int firstNumber = mainArray[0];
- for (int a = 0; a < mainArray.Length-1; a++)
- {
- mainArray[a] = mainArray[a + 1];
- }
- mainArray[mainArray.Length - 1] = firstNumber;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement