Advertisement
braveheart1989

Middle_Elements_вариант2

May 30th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _09.Middle_Elements
  8. {
  9.     class Middle_Elements
  10.     {
  11.         static void Main(string[] args)
  12.         {          
  13.             int[] length = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14.             int middleIndex = 0;
  15.             int middleIndex1 = 0;
  16.             int middleIndex2 = 0;
  17.             int counter = 0;
  18.             int counter1 = 0;
  19.             for (int i = 0; i < length.Length; i++)
  20.             {
  21.                 if (length.Length % 2 != 0 && length.Length > 1)
  22.                 {
  23.                     middleIndex = length.ElementAt(length.Length / 2 - 1);
  24.                     middleIndex1 = length.ElementAt(length.Length / 2);
  25.                     middleIndex2 = length.ElementAt((length.Length / 2) + 1);
  26.                     counter++;
  27.                 }
  28.                 else if (length.Length == 1)
  29.                 {
  30.                     middleIndex = length[0];
  31.  
  32.                 }
  33.                 else
  34.                 {
  35.                     middleIndex = length.ElementAt(length.Length / 2 - 1);
  36.                     middleIndex1 = length.ElementAt((length.Length / 2));
  37.                     counter1++;
  38.                 }
  39.             }
  40.             if (counter != 0)
  41.             {
  42.                 Console.Write("{0} {1} {2} ", middleIndex, middleIndex1, middleIndex2);
  43.             }
  44.             else if (counter == 0 && counter1 != 0)
  45.             {
  46.                 Console.Write("{0} {1} ", middleIndex, middleIndex1);
  47.             }
  48.             else
  49.             {
  50.                 Console.Write("{0} ", middleIndex);
  51.             }
  52.             Console.WriteLine();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement