Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Arrays;
- import java.util.Scanner;
- /**
- * Created by DarkSoul on 30.7.2016 г..
- */
- public class ThirteenDiagonalDifference {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Integer numOfLines = scan.nextInt();
- scan.nextLine();
- int[][] matrix = new int[numOfLines][numOfLines];
- for (int i = 0; i < numOfLines; i++) {
- int[] row = Arrays.stream(
- scan.nextLine().split(" "))
- .mapToInt(Integer::parseInt).toArray();
- for (int rows = 0; rows <row.length; rows++) {
- matrix[i][rows]=row[rows];
- }
- }
- SumDiagonals(matrix);
- // PrintingTheMatrix(matrix);
- }
- // public static void PrintingTheMatrix(int[][] matrix){
- // for (int rows = 0; rows <matrix.length ; rows++) {
- // for (int cols = 0; cols <matrix.length ; cols++) {
- // System.out.print(matrix[rows][cols]+" ");
- // }
- // System.out.println();
- // }
- // }
- public static void SumDiagonals(int[][] matrix){
- int sumOfPrimeDiag = 0;
- int sumOfSecondaryDiag = 0;
- for (int i = 0; i < matrix.length; i++) {
- sumOfPrimeDiag+=matrix[i][i];
- sumOfSecondaryDiag += (matrix[i][matrix[i].length - 1 - i]);
- }
- int diagDiff = Math.abs(sumOfPrimeDiag-sumOfSecondaryDiag);
- System.out.println(diagDiff);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement