Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MatrixBiggerSum
- {
- static void Main()
- {
- Console.WriteLine("Enter the length of the matrix rows: ");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter the length of the matrix columns: ");
- int m = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n, m];
- // Read the matrix
- Console.WriteLine("Now the values!");
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write("matrix[{0},{1}] = ", row, col);
- matrix[row, col] = int.Parse(Console.ReadLine());
- }
- Console.WriteLine();
- }
- // Find the maximal sum platform of size 3 x 3
- int count = 3; // For the sum loop count = count (3 x 3)
- int bestSum = int.MinValue;
- int bestRow = 0;
- int bestCol = 0;
- for (int row = 0; row < matrix.GetLength(0) - (count - 1); row++)
- {
- for (int col = 0; col < matrix.GetLength(1) - (count - 1); col++)
- {
- //Get the sum - Loop
- int sum = 0;
- for (int i = row; i < count + row; i++)
- {
- for (int j = col; j < count + col; j++)
- {
- sum += matrix[i, j];
- }
- }
- if (sum > bestSum)
- {
- bestSum = sum;
- bestRow = row;
- bestCol = col;
- }
- }
- }
- // Print the result
- Console.WriteLine("The best platform is:");
- for (int i = bestRow; i < count + bestRow; i++)
- {
- for (int j = bestCol; j < count + bestCol; j++)
- {
- Console.Write("{0, -4}", matrix[i, j]);
- }
- Console.WriteLine();
- }
- Console.WriteLine("The maximal sum is: {0} ", bestSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement