Advertisement
alidzhikov

StringMatrixRotation

May 8th, 2015
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class StringMatrixRotation
  5. {
  6.     static void Main()
  7.     {
  8.         string[] inputTokens = Console.ReadLine().Split(new char[] {'(', ')'}, StringSplitOptions.RemoveEmptyEntries);
  9.         int degrees = int.Parse(inputTokens[1]) % 360;
  10.  
  11.         int bestLength = int.MinValue;
  12.         Queue<string> elements = new Queue<string>();
  13.         string currentElement = Console.ReadLine();
  14.         while (!String.IsNullOrEmpty(currentElement))
  15.         {
  16.             elements.Enqueue(currentElement);
  17.             if (currentElement.Length > bestLength)
  18.             {
  19.                 bestLength = currentElement.Length;
  20.             }
  21.             currentElement = Console.ReadLine();
  22.         }
  23.  
  24.         char[,] matrix = new char[elements.Count, bestLength];
  25.         for (int row = 0; row < matrix.GetLength(0); row++)
  26.         {
  27.             currentElement = elements.Dequeue().PadRight(bestLength, ' ');
  28.             for (int col = 0; col < matrix.GetLength(1); col++)
  29.             {
  30.                 matrix[row, col] = currentElement[col];
  31.             }
  32.         }
  33.  
  34.         if (degrees == 90)
  35.         {
  36.             char[,] rotatedMatrix = new char[matrix.GetLength(1), matrix.GetLength(0)];
  37.  
  38.             for (int row = 0; row < rotatedMatrix.GetLength(0); row++)
  39.             {
  40.                 for (int col = 0; col < rotatedMatrix.GetLength(1); col++)
  41.                 {
  42.                     rotatedMatrix[row, col] = matrix[matrix.GetLength(0) - col - 1, row];
  43.                 }
  44.             }
  45.             PrintMatrix(rotatedMatrix);
  46.         }
  47.         else if (degrees == 180)
  48.         {
  49.             char[,] rotatedMatrix = new char[matrix.GetLength(0), matrix.GetLength(1)];
  50.  
  51.             for (int row = 0; row < rotatedMatrix.GetLength(0); row++)
  52.             {
  53.                 for (int col = 0; col < rotatedMatrix.GetLength(1); col++)
  54.                 {
  55.                     rotatedMatrix[row, col] = matrix[matrix.GetLength(0) - row - 1, matrix.GetLength(1) - col - 1];
  56.                 }
  57.             }
  58.             PrintMatrix(rotatedMatrix);
  59.         }
  60.         else if (degrees == 270)
  61.         {
  62.             char[,] rotatedMatrix = new char[matrix.GetLength(1), matrix.GetLength(0)];
  63.  
  64.             for (int col = 0; col < rotatedMatrix.GetLength(1); col++)
  65.             {
  66.                 for (int row = 0; row < rotatedMatrix.GetLength(0); row++)
  67.                 {
  68.                     rotatedMatrix[row, col] = matrix[col, matrix.GetLength(1) - row - 1];
  69.                 }
  70.             }
  71.             PrintMatrix(rotatedMatrix);
  72.         }
  73.         else if (degrees == 0)
  74.         {
  75.             PrintMatrix(matrix);
  76.         }
  77.     }
  78.  
  79.     private static void PrintMatrix(char[,] matrix)
  80.     {
  81.         for (int row = 0; row < matrix.GetLength(0); row++)
  82.         {
  83.             for (int col = 0; col < matrix.GetLength(1); col++)
  84.             {
  85.                 Console.Write(matrix[row, col]);
  86.             }
  87.             Console.WriteLine();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement