Advertisement
Torgach

shifting array values

Oct 15th, 2022 (edited)
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 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 Основы.Практика
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             int userInput;
  14.             int temporaryNumber;
  15.             int[] arrayNumber = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  16.  
  17.             for (int i = 0; i < arrayNumber.Length; i++)
  18.             {
  19.                 Console.Write(arrayNumber[i] + " ");
  20.             }
  21.  
  22.             Console.Write($"\nВведите количество сдвигов влево, не превышающее {arrayNumber.Length}: ");
  23.             userInput = Convert.ToInt32(Console.ReadLine());
  24.  
  25.             if(userInput % arrayNumber.Length == 0)
  26.             {
  27.                 Console.WriteLine("Введеное число некорректно!");
  28.                 return;
  29.             }
  30.  
  31.             while (userInput % arrayNumber.Length != 0)
  32.             {
  33.                 for (int i = 1; i < arrayNumber.Length; i++)
  34.                 {
  35.                     temporaryNumber = arrayNumber[i];
  36.                     arrayNumber[i] = arrayNumber[i - 1];
  37.                     arrayNumber[i - 1] = temporaryNumber;
  38.                 }
  39.  
  40.                 userInput--;
  41.             }
  42.  
  43.             Console.Write("\nНовый массив: ");
  44.             for (int i = 0; i < arrayNumber.Length; i++)
  45.             {
  46.                 Console.Write(arrayNumber[i] + " ");
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement