SvetoslavUzunov

C# For Dummies, Chapter 7, Solution 14

Mar 20th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Demo
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             int m = int.Parse(Console.ReadLine());
  13.  
  14.             var matrix = new string[n, m];
  15.             //Input
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 for (int j = 0; j < m; j++)
  19.                 {
  20.                     matrix[i, j] = Console.ReadLine();
  21.                 }
  22.             }
  23.  
  24.             //Rows
  25.             int tempCount = 1, count = 1;
  26.             string element = string.Empty;
  27.             for (int row = 0; row < n; row++)
  28.             {
  29.                 for (int col = 0; col < m - 1; col++)
  30.                 {
  31.                     if (matrix[row, col] == matrix[row, col + 1])
  32.                     {
  33.                         tempCount++;
  34.                         if (count < tempCount)
  35.                         {
  36.                             count = tempCount;
  37.                             element = matrix[row, col];
  38.                         }
  39.                     }
  40.                 }
  41.                 tempCount = 1;
  42.             }
  43.  
  44.             //Collums
  45.             for (int col = 0; col < m; col++)
  46.             {
  47.                 for (int row = 0; row < n - 1; row++)
  48.                 {
  49.                     if (matrix[row, col] == matrix[row + 1, col])
  50.                     {
  51.                         tempCount++;
  52.                         if (count < tempCount)
  53.                         {
  54.                             count = tempCount;
  55.                             element = matrix[row, col];
  56.                         }
  57.                     }
  58.                 }
  59.                 tempCount = 1;
  60.             }
  61.  
  62.             //Diagonal
  63.             for (int i = 0; i < n - 1; i++)
  64.             {
  65.                 if (matrix[i, i] == matrix[i + 1, i + 1])
  66.                 {
  67.                     tempCount++;
  68.                     if (count < tempCount)
  69.                     {
  70.                         count = tempCount;
  71.                         element = matrix[i, i];
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             //Output
  77.             while (count > 0)
  78.             {
  79.                 Console.Write($"{element} ");
  80.                 count--;
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment