Advertisement
gabi11

Multidimensional Arrays - 01. Diagonal Difference

May 12th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int dimentions = int.Parse(Console.ReadLine());
  14.  
  15.             int rows = dimentions;
  16.             int cols = dimentions;
  17.  
  18.             var matrix = new int[rows, cols];
  19.  
  20.             for (int row = 0; row < rows; row++)
  21.             {
  22.                 var currentRow = Console.ReadLine()
  23.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  24.                 .Select(int.Parse)
  25.                 .ToArray();
  26.  
  27.                 for (int col = 0; col < cols; col++)
  28.                 {
  29.                     matrix[row, col] = currentRow[col];
  30.                 }
  31.             }
  32.  
  33.             int primaryRow = 0;
  34.             int primaryCol = 0;
  35.             var primarySum = 0;
  36.  
  37.             int secondaryRow = matrix.GetLength(0) - 1;
  38.             int secondaryCol = 0;
  39.             var secondarySum = 0;
  40.  
  41.             while (true)
  42.             {
  43.                 if (primaryRow < 0
  44.                     || primaryRow >= matrix.GetLength(0)
  45.                     || primaryCol < 0
  46.                     || primaryCol >= matrix.GetLength(1))
  47.                 {
  48.                     break;
  49.                 }
  50.  
  51.                 primarySum += matrix[primaryRow, primaryCol];
  52.                 primaryRow++;
  53.                 primaryCol++;
  54.  
  55.                 secondarySum += matrix[secondaryRow, secondaryCol];
  56.                 secondaryRow--;
  57.                 secondaryCol++;
  58.             }
  59.             Console.WriteLine($"{Math.Abs(primarySum - secondarySum)}");
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement