Как сдвинуть матрицу влево? - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int n = rnd.Next(6, 6);
int m = rnd.Next(6, 6);
int[,] a = new int[n, m];
Console.WriteLine("Дана матрица {0}x{1}:", n, m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a[i, j] = rnd.Next(0, 100);
Console.Write("{0, 4}", a[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("Если хотите сдвинуть матрицу вправо введите 1, если вниз - 0");
double x = double.Parse(Console.ReadLine());
{
if (x == 1)
{
Console.WriteLine();
Console.WriteLine("Матрица передвигается вправо");
Console.WriteLine();
Console.WriteLine("Укажите величину сдвига: ");
Console.WriteLine();
int k = int.Parse(Console.ReadLine());
for (int b = 1; b <= k; b++)
for (int i = 0; i < n/2; i++)
{
int t = a[i, m - 1];
for (int j = m - 1; j > 0; j--)
a[i, j] = a[i, j - 1];
a[i, 0] = t;
}
Console.WriteLine("Сдвинутая матрица: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
Console.Write("{0, 4}", a[i, j]);
Console.WriteLine();
}
}
if (x == 0)
{
Console.WriteLine();
Console.WriteLine("Матрица передвигается вниз");
Console.WriteLine();
Console.WriteLine("Укажите величину сдвига: ");
Console.WriteLine();
int k = int.Parse(Console.ReadLine());
for (int b = 1; b <= k; b++)
for (int j = 0; j < m; j++)
{
int t = a[n - 1, j];
for (int i = n - 1; i > 0; i--)
a[i, j] = a[i - 1, j];
a[0, j] = t;
}
Console.WriteLine("Сдвинутая матрица: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
Console.Write("{0, 4}", a[i, j]);
Console.WriteLine();
}
}
if (x < 0 || x > 1)
{
//Console.ReadKey();
Console.WriteLine("введите 1 или 0");
}
Console.ReadKey();
}
}
}
}Решение задачи: «Как сдвинуть матрицу влево?»
textual
Листинг программы
for (int b = 1; b <= k; b++)
for (int i = 0; i < n; i++)
{
int t = a[i, 0];
for (int j = 0; j < m - 1; j++)
a[i, j] = a[i, j + 1];
a[i, m - 1] = t;
}