Zneeky

Квадратна Матрица

Nov 29th, 2021 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApp2
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             var matrixTxT = File.ReadAllText(@"E:\Downloads\ChromeDownloads\matrix.txt");
  14.           //  var str = matrixTxT.Split(new char[] { '\t', '\n'}, StringSplitOptions.RemoveEmptyEntries);
  15.  
  16.  
  17.             /* string[] wordsArray = matrixTxT.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  18.              foreach(string word in wordsArray)
  19.              {
  20.                  Console.Write(word+" ");
  21.              }
  22.             */
  23.  
  24.  
  25.             // var rows= File.ReadAllLines(@"E:\Downloads\ChromeDownloads\matrix.txt").Length;
  26.             // var cols =(str.Length/rows);
  27.             int rows = int.Parse(Console.ReadLine());
  28.             int cols = int.Parse(Console.ReadLine());
  29.  
  30.             int i = 0 , k = 0;
  31.  
  32.             int[,] matrix = new int[rows, cols];
  33.  
  34.       //     Console.WriteLine($"\nRows:{rows} Cols:{cols} Items:{str.Length}");
  35.  
  36.             foreach (var row in matrixTxT.Split('\n'))
  37.             {
  38.                
  39.                 string[] input = row.Split("\t");
  40.                 int r = int.Parse(input[0])-1;
  41.                 int c = int.Parse(input[1])-1;
  42.                 if (input[2].Equals(" ") || input[2].Equals(" \r"))
  43.                 {
  44.                     matrix[r, c] = -1;
  45.  
  46.                 }
  47.                 else
  48.                 {
  49.                     matrix[r, c] = int.Parse(input[2]);
  50.                 }
  51.            
  52.             }
  53.  
  54.             int evenSum = 0;
  55.             int oddSum = 0;
  56.  
  57.             int oddColSum = 0;
  58.             int evenRowSum = 0;
  59.             for (int row = 0; row < rows; row++)
  60.             {
  61.  
  62.                 for (int col = 0; col < cols; col++)
  63.                 {
  64.                     int num = matrix[row, col];
  65.                     if (num % 2 == 0)
  66.                     {
  67.                         evenSum += num;
  68.                     }
  69.                     else if(num % 2 != 0)
  70.                     {
  71.                         oddSum += num;
  72.                     }
  73.                    
  74.                     if (col % 2==0 )
  75.                     {
  76.                         oddColSum += num;
  77.                     }
  78.                     if(row % 2 != 0)
  79.                     {
  80.                         evenRowSum += num;
  81.                     }
  82.                     Console.Write(matrix[row,col]+" ");
  83.                 }
  84.                 Console.WriteLine();
  85.             }
  86.  
  87.             Console.WriteLine($"Even numbers sum: {evenSum}\nOdd numbers sum: {oddSum}");
  88.             Console.WriteLine($"Odd column sum: {oddColSum}\nEven row sum: {evenRowSum}");
  89.  
  90.  
  91.         }
  92.     }
  93. }
  94.  
  95.  
  96. /* test input
  97. 1   1   2
  98. 1   2   1
  99. 1   3    
  100. 1   4   5
  101. 2   1   2
  102. 2   2   1
  103. 2   3   7
  104. 2   4   3
  105. 3   1   2
  106. 3   2   1
  107. 3   3   4
  108. 3   4   5
  109. 4   1   3
  110. 4   2   5
  111. 4   3   6
  112. 4   4    
  113. */
Add Comment
Please, Sign In to add comment