Created
June 2, 2019 14:37
-
-
Save alldevic/9c0b61a8ec1eb15e92aa5c439175872b to your computer and use it in GitHub Desktop.
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; | |
namespace ConsoleApplication6 | |
{ | |
internal class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var n = 5; | |
var step = 2; | |
var matrix = GenerateMatrix(n, n); | |
// var matrix = new int[,] { }; | |
// | |
// var fl = false; | |
// while (!fl) | |
// { | |
// try | |
// { | |
// matrix = InputMatrix(n, n); | |
// fl = true; | |
// } | |
// catch | |
// { | |
// Console.WriteLine("Ошибка! Повторите ввод сначала"); | |
// } | |
// } | |
PrintMatrix(matrix); | |
DrawLine(); | |
CyclicShift(matrix, step, false); | |
PrintMatrix(matrix); | |
DrawLine(); | |
CyclicShift(matrix, step, true); | |
PrintMatrix(matrix); | |
} | |
private static void DrawLine() | |
{ | |
for (int i = 0; i < Console.WindowWidth; i++) | |
{ | |
Console.Write("-"); | |
} | |
Console.WriteLine(); | |
} | |
private static int[,] InputMatrix(int n, int m) | |
{ | |
var matrix = new int[n, m]; | |
try | |
{ | |
for (var i = 0; i < n; i++) | |
{ | |
for (var j = 0; j < m; j++) | |
{ | |
Console.WriteLine($"Введите [{i},{j}] элемент:"); | |
matrix[i, j] = Convert.ToInt32(Console.ReadLine()); | |
} | |
} | |
} | |
catch | |
{ | |
Console.WriteLine("Введены некорректные данные"); | |
throw; | |
} | |
return matrix; | |
} | |
public static void CyclicShift(int[,] matrix, int step, bool isDown) | |
{ | |
if (step == 0) | |
{ | |
return; | |
} | |
var length = matrix.GetLength(isDown ? 1 : 0); | |
step = length - step % length; | |
for (var i = 0; i < length; i++) | |
{ | |
// Запомнить текущую строку/столбец | |
var tmp = new int[length]; | |
for (var j = 0; j < length; j++) | |
{ | |
tmp[j] = isDown ? matrix[j, i] : matrix[i, j]; | |
} | |
// Осуществить сдвиг | |
for (var j = 0; j < length; j++) | |
{ | |
if (isDown) | |
{ | |
matrix[j, i] = tmp[(j + step) % length]; | |
} | |
else | |
{ | |
matrix[i, j] = tmp[(j + step) % length]; | |
} | |
} | |
} | |
} | |
public static int[,] GenerateMatrix(int n, int m) | |
{ | |
var matrix = new int[n, m]; | |
var rnd = new Random(); | |
for (var i = 0; i < n; i++) | |
{ | |
for (var j = 0; j < m; j++) | |
{ | |
matrix[i, j] = rnd.Next(-10, 10); | |
} | |
} | |
return matrix; | |
} | |
public static void PrintMatrix(int[,] matrix) | |
{ | |
for (var i = 0; i < matrix.GetLength(0); i++) | |
{ | |
for (var j = 0; j < matrix.GetLength(1); j++) | |
{ | |
Console.Write(matrix[i, j] + "\t"); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment