Дан двумерный массив. Переместить его элементы по периметру на 1 значение влево (против часовой стрелки) и вывести изменённый массив. Использовать динамический массив с возможностью ввести размерность.
Язык: C, C++
\left[\begin{array}{ccc}1&2&3\\4&5&6\\7&8&9\end{array}\right] \Rightarrow\left[\begin{array}{ccc}2&3&6\\1&5&9\\4&7&8\end{array}\right]\\\\\\\left[\begin{array}{ccc}1&2&3\\4&5&6\\7&8&9\\10&11&12\\13&14&15\end{array}\right] \Rightarrow\left[\begin{array}{ccc}2&3&6\\1&5&9\\4&8&12\\7&11&15\\10&13&14\end{array}\right]

Ответы

Ответ дал: SantiVlad
1

Ответ:

Код на C++:

#include <iostream>

#include <conio.h>

using namespace std;

int main()

{

int N = 1, M = 1;

cout << "Vvedite razmernost' massiva M:";

cin >> M;

cout << "Vvedite razmernost' massiva N:";

cin >> N;

int** mas;

int** mas2;

mas = new int* [M];

mas2 = new int* [M];

for (int i = 0; i < M; i++)

{

 mas[i] = new int[N];

 mas2[i] = new int[N];

}

int p = 1;

for (int i = 0; i < M; i++)

{

 for (int j = 0; j < N; j++)

 {

  mas[i][j] = p;

  p++;

 }

}

cout << "\nMassiv: \n";

for (int i = 0; i < M; i++)

{

 for (int j = 0; j < N; j++)

 {

  mas2[i][j] = mas[i][j];

  cout.width(3);

  cout << mas[i][j] << " ";

 }

 cout << "\n";

}

for (int j = 0; j < N - 1; j++)

{

 mas2[0][j] = mas[0][j + 1];

 mas2[0][N - 1] = mas[1][N - 1];

 mas2[M - 1][j + 1] = mas[M - 1][j];

 mas2[M - 1][0] = mas[M - 2][0];

}

for (int i = 0; i < M - 1; i++)

{

 mas2[i + 1][0] = mas[i][0];

 mas2[i][N - 1] = mas[i + 1][N - 1];

}

cout << "\nMassiv posle smesheniya: \n";

for (int i = 0; i < M; i++)

{

 for (int j = 0; j < N; j++)

 {

  cout.width(3);

  cout << mas2[i][j] << " ";

 }

 cout << "\n";

}

_getch();

}

///////////////////////////////////////////////////////////////////////////////////////////////

Код на C#:

static void Main(string[] args)

       {

           int N = 0, M = 0;

           Console.Write("Введите размерность массива M: ");

           M = Convert.ToInt32(Console.ReadLine());

           Console.Write("Введите размерность массива N: ");

           N = Convert.ToInt32(Console.ReadLine());

           int[,] mas = new int[M, N];

           int[,] mas2 = new int[M, N];

           int p = 1;

           for (int i = 0; i < M; i++)

           {

               for (int j = 0; j < N; j++)

               {

                   mas[i, j] = p;

                   p++;

               }

           }

           Console.WriteLine();

           Console.Write("Массив: ");

           Console.WriteLine();

           for (int i = 0; i < M; i++)

           {

               for (int j = 0; j < N; j++)

               {

                   mas2[i, j] = mas[i, j];

                   Console.Write(String.Format("{0,3}", mas[i, j]));

               }

               Console.WriteLine();

           }

           for (int j = 0; j < N - 1; j++)

           {

               mas2[0, j] = mas[0, j + 1];

               mas2[0, N - 1] = mas[1, N - 1];

               mas2[M - 1, j + 1] = mas[M - 1, j];

               mas2[M - 1, 0] = mas[M - 2, 0];

           }

           for(int i = 0; i < M - 1; i++)

           {

               mas2[i + 1, 0] = mas[i, 0];

               mas2[i, N - 1] = mas[i + 1, N - 1];

           }

           Console.WriteLine();

           Console.Write("Массив после смещения: ");

           Console.WriteLine();

           for (int i = 0; i < M; i++)

           {

               for (int j = 0; j < N; j++)

               {

                   Console.Write(String.Format("{0,3}", mas2[i, j]));

               }

               Console.WriteLine();

           }

           Console.ReadKey();

       }

Объяснение:

Код задания на двух языках. Надеюсь, помог.

Вас заинтересует