Advertisement
iWantAMcLaren

06. String Matrix Rotation

May 23rd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. String[] command = scanner.nextLine().split("Rotate\\(|\\)");
  9. int rotation = Integer.parseInt(command[1]);
  10. while (rotation >= 360) {
  11. rotation -= 360;
  12. }
  13. rotation = rotation / 90;
  14. //Razbiram kolko shte bude zavurtqna matricata
  15.  
  16. String input = "";
  17. int count = 0;
  18. List<String> str = new ArrayList<>();
  19. int length = 0;
  20. while (!"END".equals(input = scanner.nextLine())) {
  21. str.add(input);
  22. count++;
  23. if (input.length() > length) {
  24. length = input.length();
  25. }
  26. }
  27. //2. Count i Length izpolzvam za da znam (x , y) kolko na kolko shte bude golqma
  28.  
  29. char[][] matrix = new char[count][length];
  30. for (int i = 0; i < count; i++) {
  31. String x = str.get(i);
  32. char y;
  33. for (int j = 0; j < length; j++) {
  34. if (j >= x.length()) {
  35. y = ' '; //2.1 slagam space za da bude ravna matrica
  36. } else {
  37. y = x.charAt(j);
  38. }
  39. matrix[i][j] = y;
  40. }
  41. }
  42.  
  43. //3. Pulnq veche orazmerenata matrica
  44.  
  45. switch (rotation) {
  46. case 0:
  47. for (char[] chars : matrix) {
  48. System.out.println(Arrays.toString(chars).replaceAll("[\\[\\], ]", ""));
  49. }
  50. break;
  51. case 1:
  52. for (int col = 0; col < length; col++) {
  53. String print = "";
  54. for (int row = count - 1; row >= 0; row--) {
  55. print += matrix[row][col];
  56. }
  57. System.out.println(print);
  58. }
  59. break;
  60. case 2:
  61. for (int row = count - 1; row >= 0; row--) {
  62. String print = "";
  63. for (int col = length - 1; col >= 0; col--) {
  64. print += matrix[row][col];
  65. }
  66. System.out.println(print);
  67. }
  68. break;
  69. case 3:
  70. for (int col = length-1; col >= 0; col--) {
  71. String print = "";
  72. for (int row = 0; row < count; row++) {
  73. print += matrix[row][col];
  74. }
  75. System.out.println(print);
  76. }
  77. break;
  78. }
  79. //4. Printiram spored tova kak trqbva da bude zavurtqna
  80.  
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement