desislava_topuzakova

Untitled

May 30th, 2021
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. package MultidimensionalArrays;
  2.  
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. public class ReverseMatrixDiagonals_11 {
  7.  
  8. public static void main(String[] args) throws IOException {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. int[] dimensions = Arrays.stream(scanner.nextLine().split("\\s+"))
  12. .mapToInt(Integer::parseInt)
  13. .toArray();
  14.  
  15. int row = dimensions[0], col = dimensions[1];
  16.  
  17. int[][] matrix = new int[row][col];
  18.  
  19. for (int i = 0; i < row; i++) {
  20. matrix[i] = Arrays.stream(scanner.nextLine().split("\\s+"))
  21. .mapToInt(Integer::parseInt)
  22. .toArray();
  23. }
  24. row--;
  25. col--;
  26.  
  27. while (row != -1){
  28. int r = row, c = col;
  29. while (!isInbounds(r, c, matrix)){
  30. System.out.print(matrix[r--][c++] + " ");
  31. }
  32. System.out.println();
  33. col--;
  34. if(col < 0){
  35. col = 0;
  36. row--;
  37. }
  38. }
  39. }
  40.  
  41. private static boolean isInbounds(int row, int col, int [][] matrix) {
  42. return row < 0 || col < 0 || row > matrix.length - 1 || col > matrix[0].length - 1;
  43. }
  44.  
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment