Didart

Reverse Matrix Diagonals

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