Advertisement
Guest User

Untitled

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