- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
static int Determinant(int[,] matrix)
{
int length = matrix.GetLength(0);
int result = 0;
if (matrix.Length > 1)
for (int i = 0; i < length; ++i)
result += (2 * (i & 1) - 1)
* matrix[i, 0]
* Determinant(CutMatrix(matrix, i, 0));
else
result = matrix[0, 0];
return result;
}
static int[,] CutMatrix(int[,] matrix, int cutRowIndex, int cutColumnIndex)
{
int length = matrix.GetLength(0);
int[,] result = new int[length - 1, length - 1];
for (int y = 0, ry = 0; y < length; ++y, ++ry)
{
if (y != cutRowIndex)
for (int x = 0, rx = 0; x < length; ++x, ++rx)
{
if (x != cutColumnIndex)
result[ry, rx] = matrix[y, x];
else
--rx;
}
else
--ry;
}
return result;
}