Advertisement
fbinnzhivko

Diagonal Difference

Jun 8th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         var n = int.Parse(Console.ReadLine());
  8.  
  9.         long[][] matrix = new long[n][];
  10.         // Filling the Matrix
  11.         for (int row = 0; row < n; row++)
  12.         {
  13.             matrix[row] = Console.ReadLine().Split().Select(long.Parse).ToArray();
  14.         }
  15.  
  16.         // Sum the first diagonal of Matrix
  17.         long firstDiagonalSum = 0;
  18.         for (int row = 0; row < n; row++)
  19.         {
  20.             for (int col = 0; col < n; col++)
  21.             {
  22.                 if (row == col)
  23.                 {
  24.                     firstDiagonalSum += matrix[row][col];
  25.                 }
  26.             }
  27.         }
  28.  
  29.         // Sum the second diagonal of Matrix
  30.         long secondDiagonalSum = 0;
  31.         for (int row = 0; row < n; row++)
  32.         {
  33.             for (int col = n - 1; col >= 0; col--)
  34.             {
  35.                 if (col == Math.Abs(row - n + 1))
  36.                 {
  37.                     secondDiagonalSum += matrix[row][col];
  38.                 }
  39.             }
  40.         }
  41.         Console.WriteLine(Math.Abs(firstDiagonalSum - secondDiagonalSum));
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement