Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Дана матрица 4x5 . Найти минимальный элемент в строке, номер которой вводится с клавиатуры.

deFkos Fantom Ученик (127), закрыт 3 года назад
Лучший ответ
Николай Веселуха Высший разум (360666) 3 года назад
using System;
using System.Linq;
namespace Answer {
class Program {
static void Main() {
const int rows = 4;
const int cols = 5;
var matrix = CreateMatrix(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j){
Console.Write($"{matrix[i][j],5}");
}
Console.WriteLine();
}
Console.WriteLine();
Console.Write("Введите номер строки: ");
var row = int.Parse(Console.ReadLine());
var index = row - 1;
Console.WriteLine($"Минимум: {matrix[index].Min()}");
Console.ReadKey();
}
static int[][] CreateMatrix(int rows, int cols) {
var rand = new Random();
var matrix = new int[rows][];
for (int i = 0; i < rows; ++i) {
matrix[i] = new int[cols];
for (int j = 0; j < cols; ++j) {
matrix[i][j] = rand.Next(10, 100);
}
}
return matrix;
}
}
}
deFkos FantomУченик (127) 3 года назад
Спасибо!
Николай Веселуха Высший разум (360666) Пожалуйста.
Остальные ответы
Андрей Журавлев Мастер (2070) 3 года назад
using System;
using System.Linq;

namespace program
{
class Program
{
private static Random _random = new Random((int)DateTime.Now.Ticks);

static void Main()
{
var matrix = GetRandomValueMatrix(4, 5, 100, 999);

Console.WriteLine(BuildOutputString(matrix));
Console.Write("Номер строки: ");

int num = int.Parse(Console.ReadLine());
Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.WriteLine($"Минимум в строке {num}: {matrix[num - 1].Min()}");

Console.ReadKey(false);
}

static private int[][] GetRandomValueMatrix(int rowCount, int colCount, int minValue, int maxValue)
=> Enumerable.Range(0, rowCount).Select(row =>
Enumerable.Range(0, colCount).Select(cell => _random.Next(minValue, maxValue)).ToArray()
).ToArray();

static private string BuildOutputString(int[][] matrix)
=> string.Join(string.Empty,
matrix.SelectMany((row, ri) => row.Select((cell, ci)
=> (ci == 0 ? $"{ri + 1}. " : string.Empty) + cell.ToString() + (row.Length == ci + 1 ? "\r\n" : " ")))
);
}
}
Похожие вопросы