emodev

Untitled

Dec 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package LinearDataStructures.Exercises_StartSeriously_21dec;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class e06_StringMatrixRotation_Star {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         String rotation = reader.readLine();
  13.         rotation = rotation.substring(rotation.indexOf("(") + 1, rotation.length() - 1);
  14.         int rotateDegree = Integer.parseInt(rotation);
  15.  
  16.         char[][] matrix = null;
  17.         List<String> words = new ArrayList<>();
  18.         while (true) {
  19.             String input = reader.readLine();
  20.             if (input.equals("END")) {
  21.                 break;
  22.             }
  23.             words.add(input);
  24.         }
  25.  
  26.  
  27.         int maxLength = 0;
  28.         for (String word : words) {
  29.             if (word.length() > maxLength) {
  30.                 maxLength = word.length();
  31.             }
  32.         }
  33.  
  34.         matrix = new char[words.size()][maxLength];
  35.         for (int i = 0; i < words.size(); i++) {
  36.             String word = words.get(i);
  37.             for (int j = 0; j < word.length(); j++) {
  38.                 matrix[i][j] = word.charAt(j);
  39.             }
  40.             for (int j = word.length(); j < maxLength; j++) {
  41.                 matrix[i][j] = ' ';
  42.             }
  43.         }
  44. //        TODO: if statement for rotation degrees check
  45.         int rotationTimes = rotateDegree / 90;
  46.         if (rotationTimes == 1) {
  47.             matrix = rotate90(matrix);
  48.         } else if (rotationTimes % 4 == 0 && rotationTimes % 2 == 0) {
  49.             matrix = matrix;
  50.         } else if (rotationTimes % 4 == 0) {
  51.           matrix = rotate90(matrix);
  52.         } else if (rotationTimes % 2 == 0) {
  53.             matrix = rotate180(matrix);
  54.         } else if (rotationTimes % 2 != 0 && rotationTimes % 3 != 0){
  55.             matrix = rotate90(matrix);
  56.         }else if (rotationTimes % 3 == 0) {
  57.             matrix = rotate270(matrix);
  58.         }
  59.  
  60.         for (char[] chars : matrix) {
  61.             for (char letter : chars) {
  62.                 System.out.print(letter);
  63.             }
  64.             System.out.println();
  65.         }
  66.  
  67.     }
  68.  
  69.     private static char[][] rotate90(char[][] matrixToRotate) {
  70.         int row = matrixToRotate[0].length;
  71.         int col = matrixToRotate.length;
  72.  
  73.         char[][] matrix = new char[row][col];
  74.         int cols = 0;
  75.         for (int i = 0; i < row; i++) {
  76.             int colMatrixRotate = matrixToRotate.length - 1;
  77.             for (int j = 0; j < col; j++) {
  78.                 matrix[i][j] = matrixToRotate[colMatrixRotate][cols];
  79.                 colMatrixRotate--;
  80.             }
  81.             cols++;
  82.         }
  83.         return matrix;
  84.     }
  85.  
  86.     private static char[][] rotate180(char[][] matrixRotate) {
  87.         int row = matrixRotate.length;
  88.         int col = matrixRotate[0].length;
  89.  
  90.         char[][] matrix = new char[row][col];
  91.  
  92.         int rows = row - 1;
  93.         for (int i = 0; i < row; i++) {
  94.             int cols = col - 1;
  95.             for (int j = 0; j < col; j++) {
  96.                 matrix[i][cols] = matrixRotate[rows][j];
  97.                 cols--;
  98.             }
  99.             rows--;
  100.         }
  101.         return matrix;
  102.  
  103.     }
  104.  
  105.     private static char[][] rotate270(char[][] matrixRotate) {
  106.         int row = matrixRotate[0].length;
  107.         int col = matrixRotate.length;
  108.  
  109.         char[][] matrix = new char[row][col];
  110.  
  111.         int colMatrix = row - 1;
  112.         for (int i = 0; i < row; i++) {
  113.             int rowMatrix = 0;
  114.             for (int j = 0; j < col; j++) {
  115.                 matrix[i][j] = matrixRotate[rowMatrix][colMatrix];
  116.                 rowMatrix++;
  117.             }
  118.             colMatrix--;
  119.         }
  120.  
  121.         return matrix;
  122.  
  123.     }
  124. }
Add Comment
Please, Sign In to add comment