Advertisement
LemZen

Перестановка элементов массива

Jun 14th, 2018
126
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 ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Дано");
  14.             int[] arr = { 10, 20, 30, 40, 50 };//Дан массив.
  15.  
  16.             foreach (int i in arr)       //Вывод массива до перестановки элементов.
  17.             {                            //
  18.                 Console.Write($"[{i}]"); //
  19.             }                            //
  20.             Console.WriteLine($"\n");
  21.  
  22.             int length = arr.Length;//Длина массива.
  23.             int midl = length / 2;//Середина массива. длина/2
  24.             int temp;//Временная переменная для записи элемента.
  25.  
  26.             for (int i = 0; i < midl; i++)//Цыкл перестановки элементов.
  27.             {
  28.                 temp = arr[i];//Запись элемента в временную переменную.
  29.                 arr[i] = arr[length - i - 1];//Перестановка одного элемента на место другого.
  30.                 arr[length - i - 1] = temp;//Перестановка элемента из временной переменной в индекс массива.
  31.             }
  32.             Console.WriteLine("Массив после");
  33.             foreach (int i in arr)       //Цыкл вывода перестановленного массива.
  34.             {                            //
  35.                 Console.Write($"[{i}]"); //
  36.             }                            //
  37.             Console.WriteLine();
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement