Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.01 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _11._Array_Manipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] inputArray = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             string commandOrEnd = Console.ReadLine();
  12.             while (commandOrEnd != "end")
  13.             {
  14.                 string[] commandArray = commandOrEnd.Split();
  15.                 if (commandArray[0] == "exchange")
  16.                 {
  17.                     int exchangeIndex = int.Parse(commandArray[1]);
  18.                     inputArray = GetExchangedArray(exchangeIndex, inputArray);
  19.                 }
  20.                 else if (commandArray[0] == "max")
  21.                 {
  22.                     if (commandArray[1] == "even")
  23.                     {
  24.                         PrintMaxEven(inputArray);
  25.                     }
  26.                     else
  27.                     {
  28.                         PrintMaxOdd(inputArray);
  29.                     }
  30.                 }
  31.                 else if (commandArray[0] == "min")
  32.                 {
  33.                     if (commandArray[1] == "even")
  34.                     {
  35.                         PrintMinEven(inputArray);
  36.                     }
  37.                     else
  38.                     {
  39.                         PrintMinOdd(inputArray);
  40.                     }
  41.                 }
  42.                 else if (commandArray[0] == "first")
  43.                 {
  44.                     string count = commandArray[1];
  45.                     int firstCount = int.Parse(count);
  46.                     if (commandArray[2] == "even")
  47.                     {
  48.                         PrintFirstEven(inputArray,firstCount);
  49.                     }
  50.                     else if (commandArray[2] == "odd")
  51.                     {
  52.                         PrintFirstOdd(inputArray,firstCount);
  53.                     }
  54.                 }
  55.                 else if (commandArray[0] == "last")
  56.                 {
  57.                     string count = commandArray[1];
  58.                     int lastCount = int.Parse(count);
  59.                     if (commandArray[2] == "even")
  60.                     {
  61.                         PrintLastEven(inputArray,lastCount);
  62.                     }
  63.                     else if (commandArray[2] == "odd")
  64.                     {
  65.                         PrintLastOdd(inputArray, lastCount);
  66.                     }
  67.                 }
  68.                
  69.                 commandOrEnd = Console.ReadLine();
  70.             }
  71.  
  72.             PrintFinalArray(inputArray);
  73.         }
  74.  
  75.         static int[] GetExchangedArray(int exchangeIndex, int[] inputArray)
  76.         {
  77.             string arrayBeforeExchange = string.Empty;
  78.             if (exchangeIndex < 0 || inputArray.Length - 1 < exchangeIndex)
  79.             {
  80.                 Console.WriteLine("Invalid index");
  81.                 return inputArray;
  82.             }
  83.             else
  84.             {
  85.                 for (int i = exchangeIndex + 1; i < inputArray.Length; i++)
  86.                 {
  87.                     arrayBeforeExchange += inputArray[i] + " ";
  88.                 }
  89.                 for (int i = 0; i < exchangeIndex + 1; i++)
  90.                 {
  91.                     if (i == exchangeIndex)
  92.                     {
  93.                         arrayBeforeExchange += inputArray[i];
  94.                     }
  95.                     else
  96.                     {
  97.                         arrayBeforeExchange += inputArray[i] + " ";
  98.                     }
  99.                 }
  100.                 string[] arrayAfterExchange = arrayBeforeExchange.Split();
  101.                 inputArray = arrayAfterExchange.Select(int.Parse).ToArray();
  102.                 return inputArray;
  103.             }
  104.         }
  105.  
  106.         static void PrintMaxEven(int[] inputArray)
  107.         {
  108.             int bestMaxEven = int.MinValue;
  109.             int bestIndex = 0;
  110.             bool isMaxEven = false;
  111.             for (int i = 0; i < inputArray.Length; i++)
  112.             {
  113.                 int currentNum = inputArray[i];
  114.                 if (currentNum % 2 == 0)
  115.                 {
  116.                     if (bestMaxEven < currentNum)
  117.                     {
  118.                         bestMaxEven = currentNum;
  119.                         bestIndex = i;
  120.                         isMaxEven = true;
  121.                     }
  122.                 }
  123.             }
  124.             if (isMaxEven == false)
  125.             {
  126.                 Console.WriteLine("No matches");
  127.             }
  128.             else
  129.             {
  130.                 Console.WriteLine(bestIndex);
  131.             }
  132.         }
  133.  
  134.         static void PrintMaxOdd(int[] inputArray)
  135.         {
  136.             int bestMaxOdd = int.MinValue;
  137.             int bestIndex = 0;
  138.             bool isMaxOdd = false;
  139.             for (int i = 0; i < inputArray.Length; i++)
  140.             {
  141.                 int currentNum = inputArray[i];
  142.                 if (currentNum % 2 != 0)
  143.                 {
  144.                     if (bestMaxOdd < currentNum)
  145.                     {
  146.                         bestMaxOdd = currentNum;
  147.                         bestIndex = i;
  148.                         isMaxOdd = true;
  149.                     }
  150.                 }
  151.             }
  152.             if (isMaxOdd == false)
  153.             {
  154.                 Console.WriteLine("No matches");
  155.             }
  156.             else
  157.             {
  158.                 Console.WriteLine(bestIndex);
  159.             }
  160.         }
  161.  
  162.         static void PrintMinEven(int[] inputArray)
  163.         {
  164.             int bestMinEven = int.MaxValue;
  165.             int bestIndex = 0;
  166.             bool isMinEven = false;
  167.             for (int i = 0; i < inputArray.Length; i++)
  168.             {
  169.                 int currentNum = inputArray[i];
  170.                 if (currentNum % 2 == 0)
  171.                 {
  172.                     if (currentNum < bestMinEven)
  173.                     {
  174.                         bestMinEven = currentNum;
  175.                         bestIndex = i;
  176.                         isMinEven = true;
  177.                     }
  178.                 }
  179.             }
  180.             if (isMinEven == false)
  181.             {
  182.                 Console.WriteLine("No matches");
  183.             }
  184.             else
  185.             {
  186.                 Console.WriteLine(bestIndex);
  187.             }
  188.         }
  189.  
  190.         static void PrintMinOdd(int[] inputArray)
  191.         {
  192.             int bestMinOdd = int.MaxValue;
  193.             int bestIndex = 0;
  194.             bool isMinOdd = false;
  195.             for (int i = 0; i < inputArray.Length; i++)
  196.             {
  197.                 int currentNum = inputArray[i];
  198.                 if (currentNum % 2 != 0)
  199.                 {
  200.                     if (currentNum < bestMinOdd)
  201.                     {
  202.                         bestMinOdd = currentNum;
  203.                         bestIndex = i;
  204.                         isMinOdd = true;
  205.                     }
  206.                 }
  207.             }
  208.             if (isMinOdd == false)
  209.             {
  210.                 Console.WriteLine("No matches");
  211.             }
  212.             else
  213.             {
  214.                 Console.WriteLine(bestIndex);
  215.             }
  216.         }
  217.  
  218.         static void PrintFirstEven(int[] inputArray, int firstCount)
  219.         {
  220.             if (firstCount < 0 || inputArray.Length < firstCount)
  221.             {
  222.                 Console.WriteLine("Invalid count");
  223.             }
  224.             else
  225.             {
  226.                 string result = string.Empty;
  227.                 for (int i = 0; i < firstCount; i++)
  228.                 {
  229.                     int currentNumber = inputArray[i];
  230.                     if (currentNumber % 2 == 0)
  231.                     {
  232.                         result += currentNumber;
  233.                     }
  234.                 }
  235.                 Console.Write("[");
  236.                 for (int i = 0; i < result.Length; i++)
  237.                 {
  238.                     if (i == result.Length - 1)
  239.                     {
  240.                         Console.Write(result[i]);
  241.                     }
  242.                     else
  243.                     {
  244.                         Console.Write(result[i] + "," + " ");
  245.                     }
  246.                 }
  247.                 Console.Write("]");
  248.                 Console.WriteLine();
  249.             }
  250.         }
  251.  
  252.         static void PrintFirstOdd(int[]inputArray,int firstCount)
  253.         {
  254.             if (firstCount < 0 || inputArray.Length < firstCount)
  255.             {
  256.                 Console.WriteLine("Invalid count");
  257.             }
  258.             else
  259.             {
  260.                 string result = string.Empty;
  261.                 for (int i = 0; i < firstCount; i++)
  262.                 {
  263.                     int currentNumber = inputArray[i];
  264.                     if (currentNumber % 2 != 0)
  265.                     {
  266.                         result += currentNumber;
  267.                     }
  268.                 }
  269.                 Console.Write("[");
  270.                 for (int i = 0; i < result.Length; i++)
  271.                 {
  272.                     if (i == result.Length - 1)
  273.                     {
  274.                         Console.Write(result[i]);
  275.                     }
  276.                     else
  277.                     {
  278.                         Console.Write(result[i] + "," + " ");
  279.                     }
  280.                 }
  281.                 Console.Write("]");
  282.                 Console.WriteLine();
  283.             }
  284.         }
  285.  
  286.         static void PrintLastEven(int[]inputArray,int lastCount)
  287.         {
  288.             if (lastCount < 0 || inputArray.Length < lastCount)
  289.             {
  290.                 Console.WriteLine("Invalid count");
  291.             }
  292.             else
  293.             {
  294.                 string result = string.Empty;
  295.                 for (int i = inputArray.Length - lastCount; i < inputArray.Length; i++)
  296.                 {
  297.                     int currentNumber = inputArray[i];
  298.                     if (currentNumber % 2 == 0)
  299.                     {
  300.                         result += currentNumber;
  301.                     }
  302.                 }
  303.                 Console.Write("[");
  304.                 for (int i = 0; i < result.Length; i++)
  305.                 {
  306.                     if (i == result.Length - 1)
  307.                     {
  308.                         Console.Write(result[i]);
  309.                     }
  310.                     else
  311.                     {
  312.                         Console.Write(result[i] + "," + " ");
  313.                     }
  314.                 }
  315.                 Console.Write("]");
  316.                 Console.WriteLine();
  317.             }
  318.         }
  319.  
  320.         static void PrintLastOdd(int[]inputArray,int lastCount)
  321.         {
  322.             if (lastCount < 0 || inputArray.Length < lastCount)
  323.             {
  324.                 Console.WriteLine("Invalid count");
  325.             }
  326.             else
  327.             {
  328.                 string result = string.Empty;
  329.                 for (int i = inputArray.Length - lastCount; i < inputArray.Length; i++)
  330.                 {
  331.                     int currentNumber = inputArray[i];
  332.                     if (currentNumber % 2 != 0)
  333.                     {
  334.                         result += currentNumber;
  335.                     }
  336.                 }
  337.                 Console.Write("[");
  338.                 for (int i = 0; i < result.Length; i++)
  339.                 {
  340.                     if (i == result.Length - 1)
  341.                     {
  342.                         Console.Write(result[i]);
  343.                     }
  344.                     else
  345.                     {
  346.                         Console.Write(result[i] + "," + " ");
  347.                     }
  348.                 }
  349.                 Console.Write("]");
  350.                 Console.WriteLine();
  351.             }
  352.         }
  353.  
  354.         static void PrintFinalArray(int[] arrayAfterExchange)
  355.         {
  356.             string result = string.Empty;
  357.             Console.Write("[");
  358.             for (int i = 0; i < arrayAfterExchange.Length; i++)
  359.             {
  360.                 if (i == arrayAfterExchange.Length - 1)
  361.                 {
  362.                     result += arrayAfterExchange[i];
  363.                 }
  364.                 else
  365.                 {
  366.                     result += arrayAfterExchange[i] + "," + " ";
  367.                 }
  368.             }
  369.             Console.Write(result);
  370.             Console.Write("]");
  371.             Console.WriteLine();
  372.         }
  373.     }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement