Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [StartCode]
- using System;
- using System.Globalization;
- //
- // Реализуйте функцию Shuffle, которая перемешивает элементы массива в случайном порядке.
- namespace ConsoleApp2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- DisplayArray(numbers);
- int[] newNumbers = Shuffle(numbers);
- DisplayArray(newNumbers);
- }
- static int[] Shuffle(int[] array)
- {
- Random random = new Random();
- int[] newArray = new int[array.Length];
- int tempIndex;
- for (int i = 0; i < newArray.Length; i++)
- {
- tempIndex = random.Next(array.Length);
- newArray[i] = array[tempIndex];
- int[] tempArray = new int[array.Length - 1];
- for (int j = 0; j < tempIndex; j++)
- {
- tempArray[j] = array[j];
- }
- for (int j = tempIndex; j < array.Length - 1; j++)
- {
- tempArray[j] = array[j + 1];
- }
- array = tempArray;
- }
- return newArray;
- }
- static void DisplayArray(int[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write(array[i] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment