Advertisement
nevenailievaa

2201682020-Domashno2

Oct 8th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. namespace Homework2
  2. {
  3.     using System;
  4.  
  5.     class Task_2
  6.     {
  7.         {
  8.             int n = int.Parse(Console.ReadLine());          
  9.  
  10.             char[,] matrix = FillMatrix(n);
  11.  
  12.             int globalMax = 0;
  13.             char globalChar = (char)0;
  14.  
  15.             RenderRows(matrix, ref globalMax, ref globalChar);
  16.             RenderCols(matrix, ref globalMax, ref globalChar);
  17.             RenderMainDiagonal(matrix, ref globalMax, ref globalChar);
  18.             RenderSecondDiagonal(matrix, ref globalMax, ref globalChar);
  19.  
  20.             for (int i = 0; i < globalMax; i++)
  21.             {
  22.                 if (i < globalMax - 1)
  23.                 {
  24.                     Console.Write($"{globalChar} ");
  25.                 }
  26.                 else
  27.                 {
  28.                     Console.WriteLine($"{globalChar}");
  29.                 }              
  30.             }
  31.         }
  32.  
  33.  
  34.         static void RenderRows(char[,] matrix, ref int globalMax, ref char globalChar)
  35.         {          
  36.             for (int row = 0; row < matrix.GetLength(0); row++)
  37.             {
  38.                 char[] temp = new char[matrix.GetLength(1)];
  39.  
  40.                 for (int col = 0; col < matrix.GetLength(1); col++)
  41.                 {
  42.                     temp[col] = matrix[row, col];
  43.                 }
  44.  
  45.                 SetMaxSecuense(temp, ref globalMax, ref globalChar);
  46.             }
  47.         }
  48.  
  49.         static void RenderCols(char[,] matrix, ref int globalMax, ref char globalChar)
  50.         {
  51.             for (int col = 0; col < matrix.GetLength(1); col++)
  52.             {
  53.                 char[] temp = new char[matrix.GetLength(0)];
  54.  
  55.                 for (int row = 0; row < matrix.GetLength(0); row++)
  56.                 {
  57.                     temp[row] = matrix[row, col];
  58.                 }
  59.  
  60.                 SetMaxSecuense(temp, ref globalMax, ref globalChar);
  61.             }
  62.         }
  63.        
  64.         static void RenderMainDiagonal(char[,] matrix, ref int globalMax, ref char globalChar)
  65.         {
  66.             char[] temp = new char[matrix.GetLength(0)];
  67.             for (int row = 0; row < matrix.GetLength(0); row++)
  68.             {
  69.                 temp[row] = matrix[row, row];
  70.             }
  71.  
  72.             SetMaxSecuense(temp, ref globalMax, ref globalChar);
  73.         }
  74.        
  75.         static void RenderSecondDiagonal(char[,] matrix, ref int globalMax, ref char globalChar)
  76.         {
  77.             char[] temp = new char[matrix.GetLength(0)];
  78.             for (int row = 0; row < matrix.GetLength(0); row++)
  79.             {
  80.                 temp[row] = matrix[row, matrix.GetLength(0) -  1 - row];
  81.             }
  82.  
  83.             SetMaxSecuense(temp, ref globalMax, ref globalChar);
  84.         }
  85.  
  86.         static void SetMaxSecuense(char[] temp, ref int globalMax, ref char globalChar)
  87.         {
  88.             char currentChar = temp[0];
  89.             int currentMax = 1;
  90.            
  91.             for (int i = 1; i < temp.Length; i++)
  92.             {
  93.                 if (temp[i] == currentChar)
  94.                 {
  95.                     currentMax++;
  96.                 }
  97.                 else
  98.                 {
  99.                     if (currentMax > globalMax)
  100.                     {
  101.                         globalMax = currentMax;
  102.                         globalChar = currentChar;                      
  103.                     }
  104.  
  105.                     currentChar = temp[i];
  106.                     currentMax = 1;
  107.                 }
  108.             }
  109.  
  110.             if (currentMax > globalMax)
  111.             {
  112.                 globalMax = currentMax;
  113.                 globalChar = currentChar;
  114.             }
  115.         }
  116.  
  117.         static char[,] FillMatrix(int n)
  118.         {
  119.             char[,] matrix = new char[n, n];
  120.  
  121.             for (int row = 0; row < matrix.GetLength(0); row++)
  122.             {
  123.                 char[] temp = Console.ReadLine().ToCharArray();
  124.  
  125.                 for (int col = 0; col < matrix.GetLength(1); col++)
  126.                 {
  127.                     matrix[row, col] = temp[col];
  128.                 }
  129.             }
  130.  
  131.             return matrix;
  132.         }
  133.  
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement