Advertisement
IrinaIgnatova

Matrix - Reverse Matrix Diagonals

Oct 31st, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.IOException;
  5.  
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8.  
  9.  
  10. public class Main {
  11.  
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.  
  16.         int rows = scanner.nextInt();
  17.         int cols = scanner.nextInt();
  18.         scanner.nextLine();
  19.  
  20.         int[][] matrix = new int[rows][cols];
  21.  
  22.         for (int r = 0; r < rows; r++) {
  23.             matrix[r] = Arrays.stream(scanner.nextLine().split(" "))
  24.                     .mapToInt(Integer::parseInt)
  25.                     .toArray();
  26.         }
  27.  
  28.         int row = rows - 1;
  29.         int col = cols - 1;
  30.  
  31.         while (row >= 0) {
  32.             int r = row;
  33.             int c = col;
  34.  
  35.             while (c < cols && r >= 0) {
  36.                 System.out.print(matrix[r--][c++] + " ");// тук направо си принтираме резултата
  37.             }
  38.             System.out.println();
  39.  
  40.             col--;
  41.  
  42.             if (col == -1) {// ако сме на последната колона трябва да се качим един ред нагоре, но си оставаме на първа колона
  43.                 col = 0;
  44.                 row--;
  45.  
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement