Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class L06_PrintDiagonalsOfSquareMatrix {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[][] matrix = readMatrix(scanner);
- ArrayDeque<Integer> firstDiagonalQueue = new ArrayDeque<>();
- ArrayDeque<Integer> secondDiagonalStack = new ArrayDeque<>();
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix.length; j++) {
- if (i == j) {
- firstDiagonalQueue.offer(matrix[i][j]);
- }
- if (i + j == matrix.length - 1){
- secondDiagonalStack.push(matrix[i][j]);
- }
- }
- }
- while (firstDiagonalQueue.size() >= 1) {
- System.out.print(firstDiagonalQueue.pop() + " ");
- }
- System.out.println();
- while (secondDiagonalStack.size() >= 1) {
- System.out.print(secondDiagonalStack.poll() + " ");
- }
- }
- private static int[][] readMatrix(Scanner scanner) {
- int rows = Integer.parseInt(scanner.nextLine());
- int[][] matrix = new int[rows][rows];
- for (int row = 0; row < rows; row++) {
- String[] numbersToPut = scanner.nextLine().split(" ");
- for (int col = 0; col < rows; col++) {
- matrix[row][col] = Integer.parseInt(numbersToPut[col]);
- }
- }
- return matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement