Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static void Main()
- {
- var A = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
- int K = int.Parse(Console.ReadLine());
- int[] tempArray = new int[0];
- CyclicRotation(A, K);
- }
- public static int[] CyclicRotation(int[] A, int K)
- {
- //Rotate an array to the right by a given number of steps.
- // eg k= 1 A = [3, 8, 9, 7, 6] the result is [6, 3, 8, 9, 7]
- // eg k= 3 A = [3, 8, 9, 7, 6] the result is [9, 7, 6, 3, 8]
- if (A.Length == 0 || A.Length == 1)
- {
- return A;
- }
- int lastElement;
- int[] newArray = new int[A.Length];
- int[] tempArray = new int[newArray.Length];
- List<int> listOfNumbers = new List<int>();
- for (int i = 1; i < K + 1; i++)
- {
- lastElement = A[A.Length - 1];
- newArray = A.Take(A.Length - 1).ToArray();
- listOfNumbers = newArray.ToList<int>();
- listOfNumbers.Insert(0, lastElement);
- A = listOfNumbers.ToArray();
- newArray = A;
- for (int j = 0; j < tempArray.Length; j++)
- {
- tempArray[j] += newArray[j];
- }
- }
- Console.WriteLine(String.Join(" ", tempArray));
- return A;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement