C# назовем максимальный элемент двумерного массива S[n,n]- главным. Написать программу...

+415 голосов
140k просмотров

C# назовем максимальный элемент двумерного массива S[n,n]- главным. Написать программу которая находит произведение чисел той строки двумерного массива, в которой расположен главный элемент


Информатика | 140k просмотров
Дан 1 ответ
+132 голосов
Правильный ответ

Ответ:

--- C# 7.3 ---

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

namespace CSLear

{

   partial class Program

   {

       static void Main(string[] args)

       {

           int n = int.Parse(Console.ReadLine());

           int[,] Matr = MatrixRandomize(n, n, -99, 99);

           MatrixPrint(Matr);

           Console.WriteLine(Matr.GetMatrRow(IntMatrRowOfMaxIndex(Matr)).Aggregate(1f, (x, y) => x * y));

           Console.ReadKey();

       }

       private static int IntMatrRowOfMaxIndex(in int[,] Matr)

       {

           int max = int.MinValue;

           int RowOfMax = 0;

           for (int i = 0; i < Matr.GetLength(0); i++)

           {

               for (int j = 0; j < Matr.GetLength(1); j++)

               {

                   if (Matr[i, j] > max)

                   {

                       max = Matr[i, j];

                       RowOfMax = i;

                   }

               }

           }

           return RowOfMax;

       }

       public static int[,] MatrixRandomize(int ArrRows, int ArrCols, int minValue, int maxValue)

       {

           Random r = new Random();

           int[,] Arr = new int[ArrRows, ArrCols];

           for (int i = 0; i < ArrRows; i++)

           {

               for (int j = 0; j < ArrCols; j++)

               {

                   Arr[i, j] = r.Next(minValue, maxValue);

               }

           }

           return Arr;

       }

       public static void MatrixPrint(T[,] Matr)

       {

           StringBuilder sb = new StringBuilder();

           for (int i = 0; i < Matr.GetLength(0); i++)

           {

               for (int j = 0; j < Matr.GetLength(1); j++)

               {

                   sb.Append($"{Matr[i, j]} ");

               }

               sb.Append("\n");

           }

           Console.WriteLine(sb.ToString());

       }

   }

   public static class Extensions

   {

       public static T[] GetMatrRow(this T[,] Matr, int Row)

       {

           if (!typeof(T).IsPrimitive)

               throw new InvalidOperationException("Supports only primitive types");

           if (Matr == null)

               throw new NullReferenceException("Null Matrix");

           T[] Res = new T[Matr.GetLength(1)];

           int RowLength = Matr.GetLength(0);

           for (int i = 0; i < RowLength; i++)

           {

               Res[i] = Matr[Row, i];

           }

           return Res;

       }

   }

}

Программа выдаёт ответ в виде числа типа Single, т.к результат перемножения строки матрицы может сильно выходить даже за предел Int64

(1.6k баллов)
+118

Вы же не удосужились указать в задании, откуда программа должна "родить" эту вашу матрицу S[n,n], как и её размерность n

+128

Попробовали бы хоть ради интереса прочитать код, что ли. Программа ожидает ввода размерности матрицы от пользователя

+157

в консоле нет ничегоо