Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 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. class Program
  8. {
  9. static void Main()
  10. {
  11. var A = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
  12. int K = int.Parse(Console.ReadLine());
  13. int[] tempArray = new int[0];
  14. CyclicRotation(A, K);
  15. }
  16. public static int[] CyclicRotation(int[] A, int K)
  17. {
  18. //Rotate an array to the right by a given number of steps.
  19. // eg k= 1 A = [3, 8, 9, 7, 6] the result is [6, 3, 8, 9, 7]
  20. // eg k= 3 A = [3, 8, 9, 7, 6] the result is [9, 7, 6, 3, 8]
  21.  
  22.  
  23. if (A.Length == 0 || A.Length == 1)
  24. {
  25. return A;
  26. }
  27. int lastElement;
  28. int[] newArray = new int[A.Length];
  29.  
  30. int[] tempArray = new int[newArray.Length];
  31.  
  32. List<int> listOfNumbers = new List<int>();
  33.  
  34. for (int i = 1; i < K + 1; i++)
  35. {
  36.  
  37. lastElement = A[A.Length - 1];
  38. newArray = A.Take(A.Length - 1).ToArray();
  39. listOfNumbers = newArray.ToList<int>();
  40. listOfNumbers.Insert(0, lastElement);
  41.  
  42. A = listOfNumbers.ToArray();
  43. newArray = A;
  44.  
  45.  
  46. for (int j = 0; j < tempArray.Length; j++)
  47. {
  48. tempArray[j] += newArray[j];
  49. }
  50. }
  51. Console.WriteLine(String.Join(" ", tempArray));
  52. return A;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement