Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _3._Maximal_Sum
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] sizes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             int rows = sizes[0];
  13.             int cols = sizes[1];
  14.             int[,] arr = new int[rows, cols];
  15.             int[,] secondArr = new int[3, 3];
  16.             int counter = 1;
  17.             int sum = 0;
  18.             List<int> finalNumbers = new List<int>();
  19.  
  20.             for (int row = 0; row < arr.GetLength(0); row++)
  21.             {
  22.                 List<int> currentLine = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
  23.  
  24.                 for (int col = 0; col < arr.GetLength(1); col++)
  25.                 {
  26.                     arr[row, col] = currentLine[col];
  27.                 }
  28.             }
  29.  
  30.            
  31.  
  32.             for (int row = 0; row < arr.GetLength(0) - 2; row++)
  33.             {
  34.                 for (int col = 0; col < arr.GetLength(1) - 2; col++)
  35.                 {
  36.                     List<int> tempNumbers = new List<int>();
  37.  
  38.                     tempNumbers.Add(arr[row, col]);
  39.                     tempNumbers.Add(arr[row, col + 1]);
  40.                     tempNumbers.Add(arr[row, col + 2]);
  41.                     tempNumbers.Add(arr[row + 1, col]);
  42.                     tempNumbers.Add(arr[row + 1, col + 1]);
  43.                     tempNumbers.Add(arr[row + 1, col + 2]);
  44.                     tempNumbers.Add(arr[row + 2, col]);
  45.                     tempNumbers.Add(arr[row + 2, col + 1]);
  46.                     tempNumbers.Add(arr[row + 2, col + 2]);
  47.  
  48.                     if (tempNumbers.Sum() > sum)
  49.                     {
  50.                         sum = tempNumbers.Sum();
  51.  
  52.                         finalNumbers.Clear();
  53.  
  54.                         for (int i = 0; i < tempNumbers.Count; i++)
  55.                         {
  56.                             finalNumbers.Add(tempNumbers[i]);
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             Console.WriteLine($"Sum = {sum}");
  63.             for (int i = 0; i < finalNumbers.Count; i++)
  64.             {
  65.                 Console.Write(finalNumbers[i] + " ");
  66.  
  67.  
  68.                 if (counter % 3 == 0)
  69.                 {
  70.                     Console.WriteLine();
  71.                 }
  72.                 counter++;
  73.             }
  74.  
  75.  
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement