Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _11.Array_Manipulator
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.             string[] comand = Console.ReadLine().Split();
  14.             while ((comand[0]) != "end")
  15.             {
  16.  
  17.                 if (comand[0] == "exchange")
  18.                 {
  19.                     int action = int.Parse(comand[1]);
  20.                     if (action >= arr.Length || action < 0)
  21.                     {
  22.                         Console.WriteLine("Invalid index");
  23.                         comand = Console.ReadLine().Split();
  24.  
  25.                         continue;
  26.                     }
  27.                     arr = MakeArrExchange(arr, action);
  28.                 }
  29.                 else if (comand[0] == "max")
  30.                 {
  31.                     if (comand[1] == "even")
  32.                     {
  33.                         PrintMaxIndex(arr, comand[1]);
  34.                     }
  35.                     else if (comand[1] == "odd")
  36.                     {
  37.                         PrintMaxIndex(arr, comand[1]);
  38.                     }
  39.                 }
  40.                 else if (comand[0] == "min")
  41.                 {
  42.                     if (comand[1] == "even")
  43.                     {
  44.                         PrintMinIndex(arr, comand[1]);
  45.                     }
  46.                     else if (comand[1] == "odd")
  47.                     {
  48.                         PrintMinIndex(arr, comand[1]);
  49.                     }
  50.                 }
  51.                 else if (comand[0] == "first")
  52.                 {
  53.                     int count = int.Parse(comand[1]);
  54.                     if (count > arr.Length || count < 0)
  55.                     {
  56.                         Console.WriteLine("Invalid count");
  57.                         comand = Console.ReadLine().Split();
  58.                         continue;
  59.                     }
  60.                     if (comand[2] == "even")
  61.                     {
  62.                         PrintFirstElements(arr, comand[2], comand[1]);
  63.                     }
  64.                     else
  65.                     {
  66.                         PrintFirstElements(arr, comand[2], comand[1]);
  67.                     }
  68.                 }
  69.                 else if (comand[0] == "last")
  70.                 {
  71.                     int count = int.Parse(comand[1]);
  72.                     if (count > arr.Length || count < 0)
  73.                     {
  74.                         Console.WriteLine("Invalid count");
  75.                         comand = Console.ReadLine().Split();
  76.                         continue;
  77.                     }
  78.                     if (comand[2] == "even")
  79.                     {
  80.                         PrintLastElements(arr, comand[2], comand[1]);
  81.                     }
  82.                     else
  83.                     {
  84.                         PrintLastElements(arr, comand[2], comand[1]);
  85.                     }
  86.                 }
  87.                 comand = Console.ReadLine().Split();
  88.             }
  89.             Console.WriteLine("[{0}]", string.Join(", ", arr));
  90.         }
  91.  
  92.         private static void PrintLastElements(int[] arr, string comand, string count)
  93.         {
  94.             List<int> currentList = new List<int>();
  95.             int currentCount = int.Parse(count);
  96.             if (comand == "even")
  97.             {
  98.                 for (int i = arr.Length - 1; i >= 0; i--)
  99.                 {
  100.                     if (arr[i] % 2 == 0)
  101.                     {
  102.                         currentList.Add(arr[i]);
  103.                         currentCount--;
  104.                         if (currentCount < 1)
  105.                         {
  106.                             break;
  107.                         }
  108.                     }
  109.                 }
  110.                 currentList.Reverse();
  111.                 Console.WriteLine("[{0}]", string.Join(", ", currentList));
  112.             }
  113.             else
  114.             {
  115.                 for (int i = arr.Length - 1; i >= 0; i--)
  116.                 {
  117.                     if (arr[i] % 2 != 0)
  118.                     {
  119.                         currentList.Add(arr[i]);
  120.                         currentCount--;
  121.                         if (currentCount < 1)
  122.                         {
  123.                             break;
  124.                         }
  125.                     }
  126.                 }
  127.                 currentList.Reverse();
  128.                 Console.WriteLine("[{0}]",string.Join(", ", currentList));
  129.             }
  130.         }
  131.  
  132.         private static void PrintFirstElements(int[] arr, string comand, string count)
  133.         {
  134.             List<int> currentList = new List<int>();
  135.             int currentCount = int.Parse(count);
  136.             if (comand == "even")
  137.             {
  138.                 for (int i = 0; i < arr.Length; i++)
  139.                 {
  140.                     if (arr[i] % 2 == 0)
  141.                     {
  142.                         currentList.Add(arr[i]);
  143.                         currentCount--;
  144.                         if (currentCount < 1)
  145.                         {
  146.                             break;
  147.                         }
  148.                     }
  149.                 }
  150.                 Console.WriteLine("[{0}]", string.Join(", ", currentList));
  151.             }
  152.             else
  153.             {
  154.                 for (int i = 0; i < arr.Length; i++)
  155.                 {
  156.                     if (arr[i] % 2 != 0)
  157.                     {
  158.                         currentList.Add(arr[i]);
  159.                         currentCount--;
  160.                         if (currentCount < 1)
  161.                         {
  162.                             break;
  163.                         }
  164.                     }
  165.                 }
  166.                 Console.WriteLine("[{0}]",string.Join(", ", currentList));
  167.             }
  168.         }
  169.  
  170.  
  171.  
  172.         private static void PrintMinIndex(int[] arr, string comand)
  173.         {
  174.             int index = -1;
  175.             int min = int.MaxValue;
  176.             if (comand == "even")
  177.             {
  178.                 for (int i = 0; i < arr.Length; i++)
  179.                 {
  180.                     if (arr[i] % 2 == 0)
  181.                     {
  182.                         if (min >= arr[i])
  183.                         {
  184.                             min = arr[i];
  185.                             index = i;
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.             else
  191.             {
  192.                 for (int i = 0; i < arr.Length; i++)
  193.                 {
  194.                     if (arr[i] % 2 != 0)
  195.                     {
  196.                         if (min >= arr[i])
  197.                         {
  198.                             min = arr[i];
  199.                             index = i;
  200.                         }
  201.                     }
  202.                 }
  203.             }
  204.             if (index >= 0)
  205.             {
  206.                 Console.WriteLine(index);
  207.             }
  208.             else
  209.             {
  210.                 Console.WriteLine("No matches");
  211.             }
  212.         }
  213.  
  214.         private static void PrintMaxIndex(int[] arr, string comand)
  215.         {
  216.             int index = -1;
  217.             int max = int.MinValue;
  218.             if (comand == "even")
  219.             {
  220.                 for (int i = 0; i < arr.Length; i++)
  221.                 {
  222.                     if (arr[i] % 2 == 0)
  223.                     {
  224.                         if (max <= arr[i])
  225.                         {
  226.                             max = arr[i];
  227.                             index = i;
  228.                         }
  229.                     }
  230.                 }
  231.             }
  232.             else
  233.             {
  234.                 for (int i = 0; i < arr.Length; i++)
  235.                 {
  236.                     if (arr[i] % 2 != 0)
  237.                     {
  238.                         if (max <= arr[i])
  239.                         {
  240.                             max = arr[i];
  241.                             index = i;
  242.                         }
  243.                     }
  244.                 }
  245.             }
  246.  
  247.             if (index >= 0)
  248.             {
  249.                 Console.WriteLine(index);
  250.             }
  251.             else
  252.             {
  253.                 Console.WriteLine("No matches");
  254.             }
  255.         }
  256.  
  257.         private static int[] MakeArrExchange(int[] arr, int action)
  258.         {
  259.             int currentNum = 0;
  260.             for (int i = 0; i < arr.Length - 1 - action; i++)
  261.             {
  262.                 currentNum = arr[arr.Length - 1];
  263.                 for (int j = arr.Length - 1; j >= 1; j--)
  264.                 {
  265.  
  266.                     arr[j] = arr[j - 1];
  267.  
  268.                 }
  269.                 arr[0] = currentNum;
  270.             }
  271.  
  272.             return arr;
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement