Advertisement
ktopchiev

Rotate and Sum

Mar 9th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02._Rotate_and_Sum
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] arrToRotate = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  11.  
  12.             int rotationsNum = int.Parse(Console.ReadLine());
  13.  
  14.             int[] sumOfArrays = GetSumOfRotatedArrays(arrToRotate, rotationsNum);
  15.  
  16.             PrintSumOfArrays(sumOfArrays);
  17.  
  18.         }
  19.  
  20.         static int[] ArrayRightToLeftRotation(int[] arr)
  21.         {
  22.             int[] rotatedArr = new int[arr.Length];
  23.            
  24.             rotatedArr[0] = arr[arr.Length - 1];
  25.  
  26.             for (int i = 1; i < arr.Length; i++)
  27.             {
  28.                 rotatedArr[i] = arr[i - 1];
  29.             }
  30.  
  31.             return rotatedArr;
  32.         }
  33.  
  34.         static int[] GetSumOfRotatedArrays(int[] arrToRotate, int rotationsNum)
  35.         {
  36.             int[] sumOfArrays = new int[arrToRotate.Length];
  37.  
  38.             int[] rotatedArr = new int[arrToRotate.Length];
  39.  
  40.             rotatedArr = ArrayRightToLeftRotation(arrToRotate);
  41.  
  42.             for (int i = 0; i < rotationsNum; i++)
  43.             {
  44.                 for (int k = 0; k < sumOfArrays.Length; k++)
  45.                 {
  46.                     sumOfArrays[k] += rotatedArr[k];
  47.                 }
  48.  
  49.                 rotatedArr = ArrayRightToLeftRotation(rotatedArr);
  50.             }
  51.  
  52.             return sumOfArrays;
  53.         }
  54.  
  55.         static void PrintSumOfArrays(int[] sumOfArrays)
  56.         {
  57.             Console.WriteLine(string.Join(" ",sumOfArrays));
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement