Advertisement
nedelcho

Untitled

Oct 11th, 2019
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.49 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _10_TopManipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.  
  12.             string input = Console.ReadLine();
  13.  
  14.             while (input != "end")
  15.             {
  16.                 string[] inputArray = input.Split();
  17.  
  18.                 switch (inputArray[0])
  19.                 {
  20.                     case "exchange":
  21.                         Exchange(numbers, int.Parse(inputArray[1]));
  22.                         break;
  23.                     case "max":
  24.                         if (inputArray[1] == "even")
  25.                         {
  26.                             MaxEvenIndex(numbers);
  27.                         }
  28.                         else if (inputArray[1] == "odd")
  29.                         {
  30.                             MaxOddIndex(numbers);
  31.                         }
  32.                         break;
  33.                     case "min":
  34.                         if (inputArray[1] == "even")
  35.                         {
  36.                             MinEvenIndex(numbers);
  37.                         }
  38.                         else if (inputArray[1] == "odd")
  39.                         {
  40.                             MinOddIndex(numbers);
  41.                         }
  42.                         break;
  43.                     case "first":
  44.                         int count = int.Parse(inputArray[1]);
  45.                         if (count > numbers.Length)
  46.                         {
  47.                             Console.WriteLine("Invalid count");
  48.                         }
  49.                         else
  50.                         {
  51.                             if (inputArray[2] == "even")
  52.                             {
  53.                                 CountFirstEven(numbers, count);
  54.                             }
  55.                             else if (inputArray[2] == "odd")
  56.                             {
  57.                                 CountFirstOdd(numbers, count);
  58.                             }
  59.  
  60.                         }
  61.                         break;
  62.                     case "last":
  63.                         int countLast = int.Parse(inputArray[1]);
  64.                         if (countLast > numbers.Length)
  65.                         {
  66.                             Console.WriteLine("Invalid count");
  67.                         }
  68.                         else
  69.                         {
  70.                             if (inputArray[2] == "even")
  71.                             {
  72.                                 CountLastEven(numbers, countLast);
  73.                             }
  74.                             else if (inputArray[2] == "odd")
  75.                             {
  76.                                 CountLastOdd(numbers, countLast);
  77.                             }
  78.                         }
  79.                         break;
  80.                     default:
  81.                         break;
  82.                 }
  83.                 input = Console.ReadLine();
  84.             }
  85.             Console.Write("[");
  86.             for (int i = 0; i < numbers.Length; i++)
  87.             {
  88.                 if (i == 0) Console.Write(numbers[i]);
  89.                 else Console.Write($", {numbers[i]}");
  90.             }
  91.             Console.WriteLine("]");
  92.         }
  93.  
  94.         public static void Exchange(int[] numbers, int index)
  95.         {
  96.             if (index >= 0 && index < numbers.Length )
  97.             {
  98.                 int[] tempArray = new int[numbers.Length];
  99.  
  100.                 for (int i = 0; i < numbers.Length; i++)
  101.                 {
  102.                     tempArray[i] = numbers[i];
  103.                 }
  104.                 int counter = 0;
  105.  
  106.                 for (int i = index + 1; i < numbers.Length; i++)
  107.                 {
  108.                     numbers[counter] = tempArray[i];
  109.                     counter++;
  110.                 }
  111.  
  112.                 for (int i = 0; i <= index; i++)
  113.                 {
  114.                     numbers[counter] = tempArray[i];
  115.                     counter++;
  116.                 }
  117.             }
  118.  
  119.             else
  120.             {
  121.                 Console.WriteLine("Invalid index");
  122.             }
  123.         }
  124.         public static void MaxEvenIndex(int[] array)
  125.         {
  126.  
  127.             int maxEvenIndex = -1;
  128.             int maxEvenValue = int.MinValue;
  129.  
  130.             for (int i = 0; i < array.Length; i++)
  131.             {
  132.                 if (array[i] % 2 == 0 && array[i] >= maxEvenValue)
  133.                 {
  134.                     maxEvenValue = array[i];
  135.                     maxEvenIndex = i;
  136.                 }
  137.             }
  138.             if (maxEvenIndex >= 0)
  139.             {
  140.                 Console.WriteLine(maxEvenIndex);
  141.             }
  142.             else
  143.             {
  144.                 Console.WriteLine("No matches");
  145.             }
  146.  
  147.         }
  148.  
  149.         public static void MinEvenIndex(int[] array)
  150.         {
  151.             int minEvenIndex = -1;
  152.             int minEvenValue = int.MaxValue;
  153.  
  154.             for (int i = 0; i < array.Length; i++)
  155.             {
  156.                 if (array[i] % 2 == 0 && array[i] <= minEvenValue)
  157.                 {
  158.                     minEvenValue = array[i];
  159.                     minEvenIndex = i;
  160.                 }
  161.             }
  162.             if (minEvenIndex >= 0)
  163.             {
  164.                 Console.WriteLine(minEvenIndex);
  165.             }
  166.             else
  167.             {
  168.                 Console.WriteLine("No matches");
  169.             }
  170.         }
  171.  
  172.         public static void MinOddIndex(int[] array)
  173.         {
  174.             int minOddIndex = -1;
  175.             int minOddValue = int.MaxValue;
  176.  
  177.             for (int i = 0; i < array.Length; i++)
  178.             {
  179.                 if (array[i] % 2 != 0 && array[i] <= minOddValue)
  180.                 {
  181.                     minOddValue = array[i];
  182.                     minOddIndex = i;
  183.                 }
  184.             }
  185.             if (minOddIndex >= 0)
  186.             {
  187.                 Console.WriteLine(minOddIndex);
  188.             }
  189.             else
  190.             {
  191.                 Console.WriteLine("No matches");
  192.             }
  193.         }
  194.  
  195.         public static void MaxOddIndex(int[] array)
  196.         {
  197.             int maxOddIndex = -1;
  198.             int maxOddValue = int.MinValue;
  199.  
  200.             for (int i = 0; i < array.Length; i++)
  201.             {
  202.                 if (array[i] % 2 != 0 && array[i] >= maxOddValue)
  203.                 {
  204.                     maxOddValue = array[i];
  205.                     maxOddIndex = i;
  206.                 }
  207.             }
  208.             if (maxOddIndex >= 0)
  209.             {
  210.                 Console.WriteLine(maxOddIndex);
  211.             }
  212.             else
  213.             {
  214.                 Console.WriteLine("No matches");
  215.             }
  216.         }
  217.  
  218.         public static void CountFirstEven(int[] array, int count)
  219.         {
  220.             int index = 0;
  221.             Console.Write("[");
  222.             for (int i = 0; i < array.Length; i++)
  223.             {
  224.  
  225.  
  226.                 if (array[i] % 2 == 0 && index < count)
  227.                 {
  228.                     index++;
  229.                     if (index == 1) Console.Write($"{array[i]}");
  230.                     else Console.Write($", {array[i]}");
  231.                 }
  232.  
  233.             }
  234.             Console.WriteLine("]");
  235.         }
  236.  
  237.         public static void CountFirstOdd(int[] array, int count)
  238.         {
  239.             int index = 0;
  240.             Console.Write("[");
  241.             for (int i = 0; i < array.Length; i++)
  242.             {
  243.  
  244.  
  245.                 if (array[i] % 2 != 0 && index < count)
  246.                 {
  247.                     index++;
  248.                     if (index == 1) Console.Write($"{array[i]}");
  249.                     else Console.Write($", {array[i]}");
  250.                 }
  251.  
  252.             }
  253.             Console.WriteLine("]");
  254.         }
  255.  
  256.         public static void CountLastOdd(int[] array, int countLast)
  257.         {
  258.             int[] lastOddArray = new int[countLast];
  259.             int index = 0;
  260.  
  261.             for (int i = 0; i < lastOddArray.Length; i++)
  262.             {
  263.                 lastOddArray[i] = 2;
  264.             }
  265.  
  266.             for (int i = array.Length-1; i >=0 ; i--)
  267.             {
  268.                 if (array[i] % 2 != 0)
  269.                 {
  270.                     lastOddArray[index] = array[i];
  271.                     index++;
  272.                 }
  273.             }
  274.  
  275.             lastOddArray.Reverse();
  276.  
  277.             Console.Write("[");
  278.             for (int i = 0; i < lastOddArray.Length; i++)
  279.             {
  280.                 if (i == 0 && lastOddArray[i]%2 !=0) Console.Write(lastOddArray[i]);
  281.                 else if (lastOddArray[i]%2 !=0 ) Console.Write($", {lastOddArray[i]}");
  282.             }
  283.             Console.WriteLine("]");
  284.         }
  285.  
  286.         public static void CountLastEven(int[] array, int countLast)
  287.         {
  288.             int[] lastEvenArray = new int[countLast];
  289.             for (int i = 0; i < lastEvenArray.Length; i++)
  290.             {
  291.                 lastEvenArray[i] = 1;
  292.             }
  293.  
  294.             int index = 0;
  295.  
  296.             for (int i = array.Length - 1; i >= 0; i--)
  297.             {
  298.                 if (array[i] % 2 == 0)
  299.                 {
  300.                     lastEvenArray[index] = array[i];
  301.                     index++;
  302.                 }
  303.             }
  304.  
  305.             lastEvenArray.Reverse();
  306.  
  307.             Console.Write("[");
  308.             for (int i = 0; i < lastEvenArray.Length; i++)
  309.             {
  310.                 if (i == 0 && lastEvenArray[i]%2 ==0) Console.Write(lastEvenArray[i]);
  311.                 else if (lastEvenArray[i]%2==0 ) Console.Write($", {lastEvenArray[i]}");
  312.             }
  313.             Console.WriteLine("]");
  314.         }
  315.  
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement