Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package LinearDataStructures.Exercises_StartSeriously_21dec;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class e06_StringMatrixRotation_Star {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String rotation = reader.readLine();
- rotation = rotation.substring(rotation.indexOf("(") + 1, rotation.length() - 1);
- int rotateDegree = Integer.parseInt(rotation);
- char[][] matrix = null;
- List<String> words = new ArrayList<>();
- while (true) {
- String input = reader.readLine();
- if (input.equals("END")) {
- break;
- }
- words.add(input);
- }
- int maxLength = 0;
- for (String word : words) {
- if (word.length() > maxLength) {
- maxLength = word.length();
- }
- }
- matrix = new char[words.size()][maxLength];
- for (int i = 0; i < words.size(); i++) {
- String word = words.get(i);
- for (int j = 0; j < word.length(); j++) {
- matrix[i][j] = word.charAt(j);
- }
- for (int j = word.length(); j < maxLength; j++) {
- matrix[i][j] = ' ';
- }
- }
- // TODO: if statement for rotation degrees check
- int rotationTimes = rotateDegree / 90;
- if (rotationTimes == 1) {
- matrix = rotate90(matrix);
- } else if (rotationTimes % 4 == 0 && rotationTimes % 2 == 0) {
- matrix = matrix;
- } else if (rotationTimes % 4 == 0) {
- matrix = rotate90(matrix);
- } else if (rotationTimes % 2 == 0) {
- matrix = rotate180(matrix);
- } else if (rotationTimes % 2 != 0 && rotationTimes % 3 != 0){
- matrix = rotate90(matrix);
- }else if (rotationTimes % 3 == 0) {
- matrix = rotate270(matrix);
- }
- for (char[] chars : matrix) {
- for (char letter : chars) {
- System.out.print(letter);
- }
- System.out.println();
- }
- }
- private static char[][] rotate90(char[][] matrixToRotate) {
- int row = matrixToRotate[0].length;
- int col = matrixToRotate.length;
- char[][] matrix = new char[row][col];
- int cols = 0;
- for (int i = 0; i < row; i++) {
- int colMatrixRotate = matrixToRotate.length - 1;
- for (int j = 0; j < col; j++) {
- matrix[i][j] = matrixToRotate[colMatrixRotate][cols];
- colMatrixRotate--;
- }
- cols++;
- }
- return matrix;
- }
- private static char[][] rotate180(char[][] matrixRotate) {
- int row = matrixRotate.length;
- int col = matrixRotate[0].length;
- char[][] matrix = new char[row][col];
- int rows = row - 1;
- for (int i = 0; i < row; i++) {
- int cols = col - 1;
- for (int j = 0; j < col; j++) {
- matrix[i][cols] = matrixRotate[rows][j];
- cols--;
- }
- rows--;
- }
- return matrix;
- }
- private static char[][] rotate270(char[][] matrixRotate) {
- int row = matrixRotate[0].length;
- int col = matrixRotate.length;
- char[][] matrix = new char[row][col];
- int colMatrix = row - 1;
- for (int i = 0; i < row; i++) {
- int rowMatrix = 0;
- for (int j = 0; j < col; j++) {
- matrix[i][j] = matrixRotate[rowMatrix][colMatrix];
- rowMatrix++;
- }
- colMatrix--;
- }
- return matrix;
- }
- }
Add Comment
Please, Sign In to add comment