Created
October 22, 2017 18:39
-
-
Save unilecs/5f2a441cae0c20a46ebdf0ac7e5b2e1c to your computer and use it in GitHub Desktop.
Повернуть матрицу на 90 градусов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static int[][] MatrixMoveTo90(int[][] matrix) | |
{ | |
int length = matrix.Length; | |
for (int i = 0; i < length / 2; i++) | |
{ | |
int first = i; | |
int last = length - 1 - i; | |
for (int j = i; j < last; j++) | |
{ | |
int offset = j - first; | |
// сохраняем вершину | |
int top = matrix[first][j]; | |
// левая -> верхняя | |
matrix[first][j] = matrix[last - offset][first]; | |
// нижняя -> левая | |
matrix[last - offset][first] = matrix[last][last - offset]; | |
// правая -> нижняя | |
matrix[last][last - offset] = matrix[j][last]; | |
// верхняя -> правая | |
matrix[j][last] = top; | |
} | |
} | |
return matrix; | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
var matrix = new[] | |
{ | |
new[] { 1, 2, 3, 4 }, | |
new[] { 5, 6, 7, 8 }, | |
new[] { 9, 10, 11, 12 }, | |
new[] { 13, 14, 15, 16 } | |
}; | |
var newMatrix = MatrixMoveTo90(matrix); | |
foreach (int[] row in newMatrix) | |
{ | |
foreach (int element in row) | |
{ | |
Console.Write(element + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment