В заданном двумерном массиве размером N×N поменять местами элементы - C#
Формулировка задачи:
В заданном двумерном массиве размером N×N поменять местами элементы, расположенные в верхней и нижней частях массива, между главной и побочной диагоналями за исключением элементов, расположенных на этих диагоналях.
Можно таким кодом, не получается придумать алгоритм вот такого типа (rez[i, j] = mass[N - j - 1 , N - i - 1]; )
Console.WriteLine("Введите размерность: ");
int N = Convert.ToInt32(Console.ReadLine());
int[,] mass = new int[N, N];
Random random = new Random();
Console.WriteLine("Исходная мартица");
Console.WriteLine();
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
mass[i, j] = random.Next(0, 100);
Console.Write(mass[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
int[,] rez = new int[N, N];
Console.WriteLine("Результирующая мартица");
Console.WriteLine();
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
rez[i, j] = mass[N - j - 1 , N - i - 1];
Console.Write(rez[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
rez[i, j] = mass[N - j - 1 , N - i - 1] . Этот алгоритм позволяет поменять местами симметричные относительно побочной диагонали элементы местами
Мб поможет кто?
Решение задачи: «В заданном двумерном массиве размером N×N поменять местами элементы»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Text.RegularExpressions;
namespace ConsoleApplication5
{
class Program
{
static Random rnd = new Random();
static void Main(string[] args)
{
int n = 5;
int[,] array = new int[5, 5];
Console.WriteLine("before:");
for(int i = 0; i < array.GetLength(0); i++)
{
for(int j = 0; j < array.GetLength(1); j++)
{
Console.Write((array[i,j] = rnd.Next(1,10)) + " ");
}
Console.WriteLine();
}
for(int i = 0; i < array.GetLength(0); i++)
{
for(int j = i+1; j < array.GetLength(1) - i - 1; j++)
{
int tmp = array[i, j];
array[i, j] = array[array.GetLength(0) - i - 1, j];
array[array.GetLength(0) - i - 1, j] = tmp;
}
}
Console.WriteLine("after:");
for(int i = 0; i < array.GetLength(0); i++)
{
for(int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j]+ " ");
}
Console.WriteLine();
}
Console.ReadKey(true);
}
}
}