RGAlex

11. Reverse Matrix Diagonals

May 30th, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ReverseMatrixDiagonals {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int[] dim = Arrays.stream(scanner.nextLine().split(" "))
  8.                 .mapToInt(Integer::parseInt).toArray();
  9.         int dim1 = dim[0];
  10.         int dim2 = dim[1];
  11.         String[][] matrix = new String[dim1][];
  12.  
  13.         for (int i = 0; i < dim1; i++) {
  14.             String[] line = scanner.nextLine().split(" ");
  15.             matrix[i] = line;
  16.         }
  17.         int row = dim1 - 1;
  18.         int col = dim2 - 1;
  19.         while (row >= 0) {
  20.             int r = row;
  21.             int c = col;
  22.             while (c < dim2 && r >= 0) {
  23.                 System.out.print(matrix[r--][c++] + " ");
  24.             }
  25.             System.out.println();
  26.             col--;
  27.             if (col == -1) {
  28.                 col = 0;
  29.                 row--;
  30.             }
  31.         }
  32.     }
  33. }
Add Comment
Please, Sign In to add comment