Advertisement
nikeza

matrix_6.String Matrix Rotation

Aug 29th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package Advanced;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.*;
  5.  
  6. public class array_temp {
  7.  
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         String[] input = scanner.nextLine().split("\\(");
  13.         int degrees = Integer.parseInt(input[1].substring(0, input[1].length() - 1));
  14.         String command = scanner.nextLine();
  15.         List<String> word = new ArrayList<>();
  16.         int length = -1;
  17.         while (!command.equals("END")) {
  18.             word.add(command);
  19.             int tempLength = command.length();
  20.             if (tempLength > length) {
  21.                 length = tempLength;
  22.             }
  23.             command = scanner.nextLine();
  24.         }
  25.  
  26.         int row = word.size();
  27.         String[][] matrix = new String[row][];
  28.  
  29.         for (int i = 0; i < row; i++) {
  30.             String[] tokens = word.get(i).split("");
  31.             matrix[i] = tokens;
  32.         }
  33.  
  34.         int numRotate = 0;
  35.         int count = 0;
  36.         if (degrees / 90 > 0) {
  37.             for (int i = 0; i < degrees / 90; i++) {
  38.                 if (count >= 3) {
  39.                     count = 0;
  40.                 } else {
  41.                     count++;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         if (count == 1) {
  47.             ifRotate90(matrix, length);
  48.         } else if (count == 2) {
  49.             ifRotate180(matrix, length);
  50.         } else if (count == 3) {
  51.             ifRotate270(matrix, length);
  52.         } else {
  53.             for (int i = 0; i < row; i++) {
  54.                 for (int j = 0; j < length; j++) {
  55.                     try {
  56.                         System.out.print(matrix[i][j]);
  57.                     } catch (ArrayIndexOutOfBoundsException e) {
  58.                         System.out.print(" ");
  59.                     }
  60.                 }
  61.                 System.out.println();
  62.             }
  63.         }
  64. //---------
  65.     }
  66.  
  67.     private static void ifRotate180(String[][] matrix, int length) {
  68.         for (int i = matrix.length - 1; i >= 0; i--) {
  69.             for (int j = length - 1; j >= 0; j--) {
  70.                 try {
  71.                     System.out.print(matrix[i][j]);
  72.                 } catch (ArrayIndexOutOfBoundsException e) {
  73.                     System.out.print(" ");
  74.                 }
  75.             }
  76.             System.out.println();
  77.         }
  78.     }
  79.  
  80.     private static void ifRotate270(String[][] matrix, int length) {
  81.         for (int j = length - 1; j >= 0; j--) {
  82.             for (int i = 0; i < matrix.length; i++) {
  83.                 try {
  84.                     System.out.print(matrix[i][j]);
  85.                 } catch (ArrayIndexOutOfBoundsException e) {
  86.                     System.out.print(" ");
  87.                 }
  88.             }
  89.             System.out.println();
  90.         }
  91.     }
  92.  
  93.     private static void ifRotate90(String[][] matrix, int length) {
  94.  
  95.         for (int j = 0; j < length; j++) {
  96.             for (int i = matrix.length - 1; i >= 0; i--) {
  97.                 try {
  98.                     System.out.print(matrix[i][j]);
  99.                 } catch (ArrayIndexOutOfBoundsException e) {
  100.                     System.out.print(" ");
  101.                 }
  102.             }
  103.             System.out.println();
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement