Advertisement
braveheart1989

Middle_Elements

May 30th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 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 _12.Middle_Elements
  8. {
  9.     class Middle_Elements
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14.             int[] newArray = new int[2];
  15.             int[] newArray1 = new int[3];
  16.  
  17.             //int middleElement = 0;
  18.  
  19.             if (array.Length == 1)
  20.             {
  21.                 Console.WriteLine("{0}",array[0]);
  22.             }
  23.             else
  24.             {
  25.                 for (int i = 0; i < array.Length; i++)
  26.                 {
  27.  
  28.                     if (array.Length % 2 == 0)
  29.                     {
  30.                         newArray[0] = array[array.Length / 2 - 1];
  31.                         newArray[1] = array[array.Length / 2];
  32.                        
  33.                     }
  34.                     else
  35.                     {
  36.                         newArray1[0] = array[array.Length / 2 - 1];
  37.                         newArray1[1] = array[array.Length / 2];
  38.                         newArray1[2] = array[array.Length / 2 + 1];
  39.                     }
  40.                 }
  41.                 if ((array.Length % 2 == 0))
  42.                 {
  43.                     Console.WriteLine(string.Join(", ", newArray));
  44.                 }
  45.                 else
  46.                 {
  47.                     Console.WriteLine(string.Join(", ", newArray1));
  48.                 }  
  49.             }
  50.         }
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement