Advertisement
grubcho

Flip list Sides

Jun 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 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. //You will receive an integer list on the first line of the input (space-separated). Leave the first, middle and last elements as they //are. For every other element, exchange it with its opposite indexed element (list[1]  list[length-2] and so on…). After that, print //the list on the console (space-separated).
  7. namespace Flip_List_Sides
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> input = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  14.             if (input.Count % 2 ==1)
  15.             {
  16.                 for (int i = 1; i < input.Count / 2; i++)
  17.                 {
  18.                     int temp = input[i];
  19.                     input[i] = input[input.Count - (i + 1)];
  20.                     input[input.Count - (i + 1)] = temp;
  21.                 }
  22.             }
  23.             else if(input.Count % 2 == 0)
  24.             {
  25.                 for (int i = 1; i <= input.Count / 2 - 1; i++)
  26.                 {
  27.                     int temp = input[i];
  28.                     input[i] = input[input.Count - (i + 1)];
  29.                     input[input.Count - (i + 1)] = temp;
  30.                 }
  31.             }
  32.             Console.WriteLine(string.Join(" ", input));
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement