Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package multidimensionalArrays;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- public class stringMatrixRotation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split("[()]");
- String firstCmd = input[0];
- char[][] matrix = new char[0][0];
- if (firstCmd.equalsIgnoreCase("rotate")) {
- matrix = readMatrix(scanner);
- int degrees = Integer.parseInt(input[1]);
- matrix = rotateMatrix(matrix, degrees);
- }
- for (int i = 0; i < matrix.length ; i++) {
- for (int j = 0; j < matrix[i].length ; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- }
- private static char[][] rotateMatrix(char[][] matrix, int degrees) {
- int roatations = (degrees / 90) % 4;
- char[][] result = new char[0][0];
- if(roatations == 0){
- result = matrix;
- }else if(roatations == 1){
- int col = 0;
- result = new char[matrix[0].length][matrix.length];
- for (int r = matrix.length - 1; r >= 0 ; r--) {
- for (int c = 0; c < matrix[r].length ; c++) {
- result[c][col] = matrix[r][c];
- }
- col++;
- }
- }else if(roatations == 2){
- result = new char[matrix.length][matrix[0].length];
- int row = 0;
- int col = 0;
- for (int r = matrix.length - 1; r >= 0 ; r--) {
- for (int c = matrix[r].length - 1; c >= 0 ; c--) {
- result[row][col++] = matrix[r][c];
- }
- row++;
- col = 0;
- }
- }else if(roatations == 3){
- result = new char[matrix[0].length][matrix.length];
- int row = 0;
- int col = 0;
- for (int r = 0; r < matrix.length ; r++) {
- for (int c = matrix[r].length - 1; c >= 0 ; c--) {
- result[row++][col] = matrix[r][c];
- }
- col++;
- row = 0;
- }
- }
- return result;
- }
- private static char[][] readMatrix(Scanner scanner) {
- String text = scanner.nextLine();
- List<String> words = new ArrayList<>();
- int bestLen = 0;
- while(!text.equalsIgnoreCase("end")){
- if(text.length() > bestLen){
- bestLen = text.length();
- }
- words.add(text);
- text = scanner.nextLine();
- }
- char[][] matrix = new char[words.size()][bestLen];
- for (int row = 0; row < words.size() ; row++) {
- for (int col = 0; col < bestLen ; col++) {
- if(col > words.get(row).length() - 1){
- matrix[row][col] = ' ';
- }else{
- matrix[row][col] = words.get(row).charAt(col);
- }
- }
- }
- return matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement