desislava_topuzakova

06. String Matrix Rotation

Sep 22nd, 2020
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package Exercise;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class nextInt {
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scanner = new Scanner(System.in);
  11.         String rotation = scanner.nextLine();
  12.         int angleRotation = Integer.parseInt(rotation.split("[()]+")[1]) % 360;
  13.  
  14.  
  15.         ArrayList<String> linesForMatrix = new ArrayList<>();
  16.         int maxLength = 0;
  17.         while (true) {
  18.             String line = scanner.nextLine();
  19.             if(line.equals("END")){
  20.                 break;
  21.             }
  22.             linesForMatrix.add(line);
  23.             if(line.length() > maxLength){
  24.                 maxLength = line.length();
  25.             }
  26.         }
  27.  
  28.         int rows = linesForMatrix.size();
  29.         int cols = maxLength;
  30.  
  31.         char [][] matrix = new char[rows][cols];
  32.  
  33.         for (int row = 0; row < rows ; row++) {
  34.             for (int col = 0; col < cols ; col++) {
  35.                 if (col < linesForMatrix.get(row).length()) {
  36.                     matrix[row][col] = linesForMatrix.get(row).charAt(col);
  37.                } else {
  38.                     matrix[row][col] = ' ';
  39.                 }
  40.             }
  41.         }
  42.  
  43.         rotate(angleRotation, rows, cols, matrix);
  44.  
  45.  
  46.     }
  47.  
  48.     private static void rotate(int angleRotation, int rows, int cols, char[][] matrix) {
  49.         if (angleRotation == 90) {
  50.             for (int col = 0; col < cols ; col++) {
  51.                 for (int row = rows - 1; row >= 0 ; row--) {
  52.                     System.out.print(matrix[row][col]);
  53.                 }
  54.                 System.out.println();
  55.             }
  56.  
  57.         } else if (angleRotation == 180) {
  58.             for (int row = rows - 1; row >= 0 ; row--) {
  59.                 for (int col = cols - 1; col >= 0 ; col--) {
  60.                     System.out.print(matrix[row][col]);
  61.                 }
  62.                 System.out.println();
  63.             }
  64.         } else if (angleRotation == 270) {
  65.             for (int col = cols - 1; col >= 0 ; col--) {
  66.                 for (int row = 0; row < rows ; row++) {
  67.                     System.out.print(matrix[row][col]);
  68.  
  69.                 }
  70.                 System.out.println();
  71.             }
  72.         } else {
  73.             for (int row = 0; row < rows ; row++) {
  74.                 for (int col = 0; col < cols ; col++) {
  75.                     System.out.print(matrix[row][col]);
  76.                 }
  77.                 System.out.println();
  78.             }
  79.         }
  80.     }
  81.  
  82.     private static void printMatrix(char[][] matrix) {
  83.         for (int row = 0; row < matrix.length ; row++) {
  84.             for (int col = 0; col < matrix[row].length; col++) {
  85.                 System.out.print(matrix[row][col] + " ");
  86.             }
  87.             System.out.println();
  88.         }
  89.     }
  90.  
  91.     public static void fillMatrixType1(Scanner scanner, int[][] matrix, int rows, int cols) {
  92.         for (int row = 0; row < rows; row++) {
  93.             matrix[row] = Arrays.stream(scanner.nextLine().split("\\s+"))
  94.                     .mapToInt(Integer::parseInt).toArray();
  95.         }
  96.     }
  97. }
Add Comment
Please, Sign In to add comment