Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[] dimensions = getDimensions(scanner);
- int rows = dimensions[0];
- int cols = dimensions[1];
- int counter = 0;
- int[][] matrix = getMatrix(scanner, rows, cols);
- LinkedHashMap<Integer, ArrayList<Integer>> map = new LinkedHashMap<>();
- for (int r = 0; r < matrix.length; r++) {
- counter = r;
- for (int c = 0; c < matrix[0].length; c++) {
- if (c + r == counter) {
- map.putIfAbsent(c + r, new ArrayList<>());
- map.get(c + r).add(matrix[r][c]);
- }
- counter++;
- }
- }
- LinkedHashMap<Integer, ArrayList<Integer>> newMap = new LinkedHashMap<>(map.size());
- for (int i = 0; i < map.size(); i++) {
- newMap.putIfAbsent(i, map.get(map.size() - 1 - i));
- }
- for (ArrayList<Integer> value : newMap.values()) {
- Collections.sort(value);
- for (int j = 0; j <= value.size() - 1; j++) {
- System.out.print(value.get(j) + " ");
- }
- System.out.println();
- }
- }
- private static int[][] getMatrix(Scanner scanner, int rows, int cols) {
- int[][] matrix = new int[rows][cols];
- for (int r = 0; r < matrix.length; r++) {
- int[] arr = Arrays.stream(scanner.nextLine().split(" "))
- .mapToInt(Integer::parseInt)
- .toArray();
- matrix[r] = arr;
- }
- return matrix;
- }
- private static int[] getDimensions(Scanner scanner) {
- int[] dimensions = Arrays.stream(scanner.nextLine().split(" "))
- .mapToInt(Integer::parseInt)
- .toArray();
- return dimensions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement