kwest87

Untitled

Nov 30th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. [StartCode]
  2. using System;
  3. using System.Globalization;
  4.  
  5. //
  6. // Реализуйте функцию Shuffle, которая перемешивает элементы массива в случайном порядке.
  7.  
  8. namespace ConsoleApp2
  9. {
  10.     internal class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  15.             DisplayArray(numbers);
  16.             int[] newNumbers = Shuffle(numbers);
  17.             DisplayArray(newNumbers);
  18.         }
  19.  
  20.         static int[] Shuffle(int[] array)
  21.         {
  22.             Random random = new Random();
  23.             int[] newArray = new int[array.Length];
  24.             int tempIndex;
  25.  
  26.             for (int i = 0; i < newArray.Length; i++)
  27.             {
  28.                 tempIndex = random.Next(array.Length);
  29.                 newArray[i] = array[tempIndex];
  30.                 int[] tempArray = new int[array.Length - 1];
  31.  
  32.                 for (int j = 0; j < tempIndex; j++)
  33.                 {
  34.                     tempArray[j] = array[j];
  35.                 }
  36.  
  37.                 for (int j = tempIndex; j < array.Length - 1; j++)
  38.                 {
  39.                     tempArray[j] = array[j + 1];
  40.                 }
  41.  
  42.                 array = tempArray;
  43.             }
  44.  
  45.             return newArray;
  46.         }
  47.  
  48.         static void DisplayArray(int[] array)
  49.         {
  50.             for (int i = 0; i < array.Length; i++)
  51.             {
  52.                 Console.Write(array[i] + " ");
  53.             }
  54.  
  55.             Console.WriteLine();
  56.         }
  57.     }
  58. }
  59. [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment