Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _10_TopManipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
- string input = Console.ReadLine();
- while (input != "end")
- {
- string[] inputArray = input.Split();
- switch (inputArray[0])
- {
- case "exchange":
- Exchange(numbers, int.Parse(inputArray[1]));
- break;
- case "max":
- if (inputArray[1] == "even")
- {
- MaxEvenIndex(numbers);
- }
- else if (inputArray[1] == "odd")
- {
- MaxOddIndex(numbers);
- }
- break;
- case "min":
- if (inputArray[1] == "even")
- {
- MinEvenIndex(numbers);
- }
- else if (inputArray[1] == "odd")
- {
- MinOddIndex(numbers);
- }
- break;
- case "first":
- int count = int.Parse(inputArray[1]);
- if (count > numbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- if (inputArray[2] == "even")
- {
- CountFirstEven(numbers, count);
- }
- else if (inputArray[2] == "odd")
- {
- CountFirstOdd(numbers, count);
- }
- }
- break;
- case "last":
- int countLast = int.Parse(inputArray[1]);
- if (countLast > numbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- if (inputArray[2] == "even")
- {
- CountLastEven(numbers, countLast);
- }
- else if (inputArray[2] == "odd")
- {
- CountLastOdd(numbers, countLast);
- }
- }
- break;
- default:
- break;
- }
- input = Console.ReadLine();
- }
- Console.Write("[");
- for (int i = 0; i < numbers.Length; i++)
- {
- if (i == 0) Console.Write(numbers[i]);
- else Console.Write($", {numbers[i]}");
- }
- Console.WriteLine("]");
- }
- public static void Exchange(int[] numbers, int index)
- {
- if (index >= 0 && index < numbers.Length )
- {
- int[] tempArray = new int[numbers.Length];
- for (int i = 0; i < numbers.Length; i++)
- {
- tempArray[i] = numbers[i];
- }
- int counter = 0;
- for (int i = index + 1; i < numbers.Length; i++)
- {
- numbers[counter] = tempArray[i];
- counter++;
- }
- for (int i = 0; i <= index; i++)
- {
- numbers[counter] = tempArray[i];
- counter++;
- }
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- }
- public static void MaxEvenIndex(int[] array)
- {
- int maxEvenIndex = -1;
- int maxEvenValue = int.MinValue;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && array[i] >= maxEvenValue)
- {
- maxEvenValue = array[i];
- maxEvenIndex = i;
- }
- }
- if (maxEvenIndex >= 0)
- {
- Console.WriteLine(maxEvenIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- public static void MinEvenIndex(int[] array)
- {
- int minEvenIndex = -1;
- int minEvenValue = int.MaxValue;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && array[i] <= minEvenValue)
- {
- minEvenValue = array[i];
- minEvenIndex = i;
- }
- }
- if (minEvenIndex >= 0)
- {
- Console.WriteLine(minEvenIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- public static void MinOddIndex(int[] array)
- {
- int minOddIndex = -1;
- int minOddValue = int.MaxValue;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && array[i] <= minOddValue)
- {
- minOddValue = array[i];
- minOddIndex = i;
- }
- }
- if (minOddIndex >= 0)
- {
- Console.WriteLine(minOddIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- public static void MaxOddIndex(int[] array)
- {
- int maxOddIndex = -1;
- int maxOddValue = int.MinValue;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && array[i] >= maxOddValue)
- {
- maxOddValue = array[i];
- maxOddIndex = i;
- }
- }
- if (maxOddIndex >= 0)
- {
- Console.WriteLine(maxOddIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- public static void CountFirstEven(int[] array, int count)
- {
- int index = 0;
- Console.Write("[");
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && index < count)
- {
- index++;
- if (index == 1) Console.Write($"{array[i]}");
- else Console.Write($", {array[i]}");
- }
- }
- Console.WriteLine("]");
- }
- public static void CountFirstOdd(int[] array, int count)
- {
- int index = 0;
- Console.Write("[");
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && index < count)
- {
- index++;
- if (index == 1) Console.Write($"{array[i]}");
- else Console.Write($", {array[i]}");
- }
- }
- Console.WriteLine("]");
- }
- public static void CountLastOdd(int[] array, int countLast)
- {
- int[] lastOddArray = new int[countLast];
- int index = 0;
- for (int i = 0; i < lastOddArray.Length; i++)
- {
- lastOddArray[i] = 2;
- }
- for (int i = array.Length-1; i >=0 ; i--)
- {
- if (array[i] % 2 != 0)
- {
- lastOddArray[index] = array[i];
- index++;
- }
- }
- lastOddArray.Reverse();
- Console.Write("[");
- for (int i = 0; i < lastOddArray.Length; i++)
- {
- if (i == 0 && lastOddArray[i]%2 !=0) Console.Write(lastOddArray[i]);
- else if (lastOddArray[i]%2 !=0 ) Console.Write($", {lastOddArray[i]}");
- }
- Console.WriteLine("]");
- }
- public static void CountLastEven(int[] array, int countLast)
- {
- int[] lastEvenArray = new int[countLast];
- for (int i = 0; i < lastEvenArray.Length; i++)
- {
- lastEvenArray[i] = 1;
- }
- int index = 0;
- for (int i = array.Length - 1; i >= 0; i--)
- {
- if (array[i] % 2 == 0)
- {
- lastEvenArray[index] = array[i];
- index++;
- }
- }
- lastEvenArray.Reverse();
- Console.Write("[");
- for (int i = 0; i < lastEvenArray.Length; i++)
- {
- if (i == 0 && lastEvenArray[i]%2 ==0) Console.Write(lastEvenArray[i]);
- else if (lastEvenArray[i]%2==0 ) Console.Write($", {lastEvenArray[i]}");
- }
- Console.WriteLine("]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement