Advertisement
ktopchiev

02. Rotate and Sum

Mar 9th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 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[] sumArr = GetSumOfRotatedArrays(arrToRotate, rotationsNum);
  15.  
  16.             PrintSumOfArrays(sumArr);
  17.  
  18.         }
  19.  
  20.         static int[] ArrayRightToLeftRotation(int[] arr)
  21.         {
  22.             int[] rotatedArr = new int[arr.Length];
  23.  
  24.             for (int rotate = 0; rotate < 1; rotate++)
  25.             {
  26.                 rotatedArr[rotate] = arr[arr.Length - 1 - rotate];
  27.                 for (int i = 0; i < arr.Length - 1; i++)
  28.                 {
  29.                     rotatedArr[i + 1] = arr[i];
  30.                 }
  31.             }
  32.             return rotatedArr;
  33.         }
  34.  
  35.         static int[] GetSumOfRotatedArrays(int[] arrToRotate, int rotationsNum)
  36.         {
  37.             int[] sumArr = new int[arrToRotate.Length];
  38.  
  39.             int[] rotatedArr = new int[arrToRotate.Length];
  40.             rotatedArr = ArrayRightToLeftRotation(arrToRotate);
  41.  
  42.             for (int i = 0; i < rotationsNum; i++)
  43.             {
  44.                 for (int k = 0; k < sumArr.Length; k++)
  45.                 {
  46.                     sumArr[k] = sumArr[k] + rotatedArr[k];
  47.                 }
  48.  
  49.                 rotatedArr = ArrayRightToLeftRotation(rotatedArr);
  50.             }
  51.  
  52.             return sumArr;
  53.         }
  54.  
  55.         static void PrintSumOfArrays(int[] sumArr)
  56.         {
  57.             foreach (var num in sumArr)
  58.             {
  59.                 Console.Write($"{num} ");
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement