Advertisement
Guest User

Diagonal Difference

a guest
Jul 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Created by DarkSoul on 30.7.2016 г..
  8. */
  9. public class ThirteenDiagonalDifference {
  10.  
  11. public static void main(String[] args) {
  12. Scanner scan = new Scanner(System.in);
  13. Integer numOfLines = scan.nextInt();
  14. scan.nextLine();
  15.  
  16. int[][] matrix = new int[numOfLines][numOfLines];
  17. for (int i = 0; i < numOfLines; i++) {
  18. int[] row = Arrays.stream(
  19. scan.nextLine().split(" "))
  20. .mapToInt(Integer::parseInt).toArray();
  21. for (int rows = 0; rows <row.length; rows++) {
  22. matrix[i][rows]=row[rows];
  23.  
  24. }
  25. }
  26. SumDiagonals(matrix);
  27.  
  28. // PrintingTheMatrix(matrix);
  29.  
  30.  
  31. }
  32.  
  33.  
  34. // public static void PrintingTheMatrix(int[][] matrix){
  35. // for (int rows = 0; rows <matrix.length ; rows++) {
  36. // for (int cols = 0; cols <matrix.length ; cols++) {
  37. // System.out.print(matrix[rows][cols]+" ");
  38. // }
  39. // System.out.println();
  40. // }
  41. // }
  42.  
  43. public static void SumDiagonals(int[][] matrix){
  44. int sumOfPrimeDiag = 0;
  45. int sumOfSecondaryDiag = 0;
  46. for (int i = 0; i < matrix.length; i++) {
  47. sumOfPrimeDiag+=matrix[i][i];
  48. sumOfSecondaryDiag += (matrix[i][matrix[i].length - 1 - i]);
  49. }
  50.  
  51. int diagDiff = Math.abs(sumOfPrimeDiag-sumOfSecondaryDiag);
  52. System.out.println(diagDiff);
  53. }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement