Advertisement
deyanmalinov

03. Diagonal Difference

May 25th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args){
  5.         Scanner scan = new Scanner(System.in);
  6.         int matSize = Integer.parseInt(scan.nextLine());
  7.         int[][] matrix = new int[matSize][matSize];
  8.         int sum = 0; int sum2 = 0;
  9.         for (int row = 0; row < matSize; row++) {
  10.             String[] line = scan.nextLine().split(" ");
  11.             for (int col = 0; col < matSize; col++) {
  12.                 matrix[row][col] = Integer.parseInt(line[col]);
  13.             }
  14.         }
  15.         for (int col = 0; col < matSize; col++) {
  16.                 sum = matrix[col][col] + sum;
  17.             }
  18.         for (int col = 0; col < matSize; col++) {
  19.             sum2 = matrix[col][matSize-1-col] + sum2;
  20.         }
  21.         if (sum>sum2){
  22.             System.out.println(sum-sum2);
  23.         }else {
  24.             System.out.println(sum2-sum);
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement