Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2019
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. int[] dimensions = getDimensions(scanner);
  12. int rows = dimensions[0];
  13. int cols = dimensions[1];
  14. int counter = 0;
  15. int[][] matrix = getMatrix(scanner, rows, cols);
  16. LinkedHashMap<Integer, ArrayList<Integer>> map = new LinkedHashMap<>();
  17. for (int r = 0; r < matrix.length; r++) {
  18. counter = r;
  19. for (int c = 0; c < matrix[0].length; c++) {
  20. if (c + r == counter) {
  21. map.putIfAbsent(c + r, new ArrayList<>());
  22. map.get(c + r).add(matrix[r][c]);
  23. }
  24. counter++;
  25. }
  26. }
  27. LinkedHashMap<Integer, ArrayList<Integer>> newMap = new LinkedHashMap<>(map.size());
  28. for (int i = 0; i < map.size(); i++) {
  29. newMap.putIfAbsent(i, map.get(map.size() - 1 - i));
  30. }
  31. for (ArrayList<Integer> value : newMap.values()) {
  32. Collections.sort(value);
  33. for (int j = 0; j <= value.size() - 1; j++) {
  34. System.out.print(value.get(j) + " ");
  35. }
  36. System.out.println();
  37.  
  38. }
  39. }
  40.  
  41. private static int[][] getMatrix(Scanner scanner, int rows, int cols) {
  42. int[][] matrix = new int[rows][cols];
  43. for (int r = 0; r < matrix.length; r++) {
  44. int[] arr = Arrays.stream(scanner.nextLine().split(" "))
  45. .mapToInt(Integer::parseInt)
  46. .toArray();
  47. matrix[r] = arr;
  48. }
  49. return matrix;
  50. }
  51.  
  52. private static int[] getDimensions(Scanner scanner) {
  53. int[] dimensions = Arrays.stream(scanner.nextLine().split(" "))
  54. .mapToInt(Integer::parseInt)
  55. .toArray();
  56. return dimensions;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement