SHARE
TWEET

03. Diagonal Difference

deyanmalinov May 25th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top