JulianJulianov

04. Array Rotation

Feb 5th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. 04. Array Rotation
  2. Write a program that receives an array and number of rotations you have to perform (first element goes at the end) Print the resulting array.
  3. Examples
  4. Input Output
  5. 51 47 32 61 21 32 61 21 51 47
  6. 2
  7. 32 21 61 1 32 21 61 1
  8. 4
  9. 2 4 15 31 4 15 31 2
  10. 5
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17.  
  18. namespace _04ArrayRotation
  19. {
  20. class Program
  21. {
  22. static void Main(string[] args)
  23. {
  24. string[] array = Console.ReadLine().Split();
  25. var counter = int.Parse(Console.ReadLine());
  26.  
  27. for (int a = 0; a < counter; a++)
  28. {
  29. string temporary = array[0];//string temporary = array[array.Length - 1]
  30. for (int i = 0; i < array.Length - 1; i++)//for (int i = array.Length - 1; i > 0; i--)
  31. { // Така ще ги завърти като последния елемент става първи.
  32. array[i] = array[i + 1];//array[i] = array[i - 1];
  33. }
  34. array[array.Length - 1] = temporary;//array[0] = temporary;
  35. }
  36. Console.WriteLine(string.Join(" ", array));
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment