Skip to content

Instantly share code, notes, and snippets.

@Andrealisreal
Created February 4, 2025 18:11
Show Gist options
  • Save Andrealisreal/b896b3e9fa6453f70b4354567d6fe517 to your computer and use it in GitHub Desktop.
Save Andrealisreal/b896b3e9fa6453f70b4354567d6fe517 to your computer and use it in GitHub Desktop.
Сдвиг значений массива
using System;
namespace ConsoleApp
{
public class MainClass
{
public static void Main(string[] args)
{
Random random = new Random();
int minRandomValue = 1;
int maxRandomValue = 11;
int numberOfElements = 5;
int[] randomNumbers = new int[numberOfElements];
for (int i = 0; i < randomNumbers.Length; i++)
randomNumbers[i] = random.Next(minRandomValue, maxRandomValue);
Console.Write("Изначальный массив: ");
for (int i = 0; i < randomNumbers.Length; i++)
Console.Write($"{randomNumbers[i]} ");
Console.Write("\nВведите количество сдвигов: ");
int shift = Convert.ToInt32(Console.ReadLine());
shift %= randomNumbers.Length;
for (int j = 0; j < shift; j++)
{
int firstElement = randomNumbers[0];
for (int i = 0; i < randomNumbers.Length - 1; i++)
randomNumbers[i] = randomNumbers[i + 1];
randomNumbers[randomNumbers.Length - 1] = firstElement;
}
Console.Write("Сдвинутый массив: ");
for (int i = 0; i < randomNumbers.Length; i++)
Console.Write($"{randomNumbers[i]} ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment