Дан двумерный массив (матрица) размером NxN.Преобразовать матрицу, заполнив главную диагональ значениями минимальных элементов ее строк.Можно ли еще уменьшить код?
Код F#
Код:
// Учебный шаблон для матричных типов
type Matrix<'T>(N: int, M: int) =
let internalArray = Array2D.zeroCreate<'T> N M
member this.Item
with get(a: int, b: int) = internalArray.[a, b]
and set(a: int, b: int) (value:'T) = internalArray.[a, b] <- value
member this.GetSlice(rowStart: int option, rowFinish : int option,
colStart: int option, colFinish : int option) =
let rowStart = match rowStart with
| Some(v) -> v
| None -> 0
let rowFinish = match rowFinish with
| Some(v) -> v
| None -> internalArray.GetLength(0) - 1
let colStart = match colStart with
| Some(v) -> v
| None -> 0
let colFinish = match colFinish with
| Some(v) -> v
| None -> internalArray.GetLength(1) - 1
internalArray.[rowStart..rowFinish, colStart..colFinish]
member this.GetSlice(row: int, colStart: int option, colFinish: int option) =
let colStart = match colStart with
| Some(v) -> v
| None -> 0
let colFinish = match colFinish with
| Some(v) -> v
| None -> internalArray.GetLength(1) - 1
internalArray.[row, colStart..colFinish]
member this.GetSlice(rowStart: int option, rowFinish: int option, col: int) =
let rowStart = match rowStart with
| Some(v) -> v
| None -> 0
let rowFinish = match rowFinish with
| Some(v) -> v
| None -> internalArray.GetLength(0) - 1
internalArray.[rowStart..rowFinish, col]
//Дан двумерный массив (матрица) размером NxN.Преобразовать матрицу, заполнив главную диагональ значениями минимальных элементов ее строк.
open System
let n = Int32.Parse <<System.Console.ReadLine
let getN =
n ()
System.Console.Write("n=")
System.Console.WriteLine(getN)
System.Console.WriteLine("Генерация матрицы");
//=======================================================================================
module test =
let generateTestMatrix =
let rnd = System.Random()
let matrix = new Matrix<float>(getN+1, getN+1);
for i in 0..getN do
for j in 0..getN do
matrix.[i, j] <-Math.Round(rnd.NextDouble()*100.0,2)
matrix
printfn "%A" generateTestMatrix.[0..getN,0..getN];
System.Console.WriteLine("Преобразованная матрица");
let generateTestMatrix1 =
let rnd = System.Random()
let matrix = new Matrix<float>(getN+1, getN+1);
for i in 0..getN do
for j in 0..getN do
matrix.[i, j] <-Array.min generateTestMatrix.[i,*]
matrix
let generateTestMatrix2 =
let matrix = new Matrix<float>(getN+1, getN+1);
for i in 0..getN do
for j in 0..getN do
matrix.[i, j] <-if (i = j) then generateTestMatrix1.[i,j] else generateTestMatrix.[i,j]
matrix
module test2 =
printfn "%A" generateTestMatrix2.[0..getN,0..getN]