Advertisement
Guest User

MatrixRotation

a guest
Jan 27th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package multidimensionalArrays;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class stringMatrixRotation {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. String[] input = scanner.nextLine().split("[()]");
  12. String firstCmd = input[0];
  13. char[][] matrix = new char[0][0];
  14.  
  15. if (firstCmd.equalsIgnoreCase("rotate")) {
  16. matrix = readMatrix(scanner);
  17. int degrees = Integer.parseInt(input[1]);
  18. matrix = rotateMatrix(matrix, degrees);
  19. }
  20.  
  21. for (int i = 0; i < matrix.length ; i++) {
  22. for (int j = 0; j < matrix[i].length ; j++) {
  23. System.out.print(matrix[i][j]);
  24. }
  25. System.out.println();
  26. }
  27.  
  28. }
  29.  
  30. private static char[][] rotateMatrix(char[][] matrix, int degrees) {
  31. int roatations = (degrees / 90) % 4;
  32. char[][] result = new char[0][0];
  33.  
  34. if(roatations == 0){
  35. result = matrix;
  36. }else if(roatations == 1){
  37. int col = 0;
  38. result = new char[matrix[0].length][matrix.length];
  39.  
  40. for (int r = matrix.length - 1; r >= 0 ; r--) {
  41. for (int c = 0; c < matrix[r].length ; c++) {
  42. result[c][col] = matrix[r][c];
  43. }
  44. col++;
  45. }
  46.  
  47. }else if(roatations == 2){
  48. result = new char[matrix.length][matrix[0].length];
  49. int row = 0;
  50. int col = 0;
  51. for (int r = matrix.length - 1; r >= 0 ; r--) {
  52. for (int c = matrix[r].length - 1; c >= 0 ; c--) {
  53. result[row][col++] = matrix[r][c];
  54. }
  55. row++;
  56. col = 0;
  57. }
  58.  
  59. }else if(roatations == 3){
  60. result = new char[matrix[0].length][matrix.length];
  61. int row = 0;
  62. int col = 0;
  63.  
  64. for (int r = 0; r < matrix.length ; r++) {
  65. for (int c = matrix[r].length - 1; c >= 0 ; c--) {
  66. result[row++][col] = matrix[r][c];
  67. }
  68. col++;
  69. row = 0;
  70. }
  71.  
  72. }
  73.  
  74. return result;
  75. }
  76.  
  77. private static char[][] readMatrix(Scanner scanner) {
  78. String text = scanner.nextLine();
  79. List<String> words = new ArrayList<>();
  80.  
  81. int bestLen = 0;
  82. while(!text.equalsIgnoreCase("end")){
  83. if(text.length() > bestLen){
  84. bestLen = text.length();
  85. }
  86. words.add(text);
  87. text = scanner.nextLine();
  88. }
  89.  
  90. char[][] matrix = new char[words.size()][bestLen];
  91. for (int row = 0; row < words.size() ; row++) {
  92. for (int col = 0; col < bestLen ; col++) {
  93. if(col > words.get(row).length() - 1){
  94. matrix[row][col] = ' ';
  95. }else{
  96. matrix[row][col] = words.get(row).charAt(col);
  97. }
  98. }
  99.  
  100. }
  101.  
  102. return matrix;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement