Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace E7.Matrix_Generator
- {
- class MatrixGenerator
- {
- static void Main(string[] args)
- {
- string[] input = Console.ReadLine().Split();
- int rows = int.Parse(input[1]);
- int cols = int.Parse(input[2]);
- switch (input[0])
- {
- case "A": TypeA(rows, cols); break;
- case "B": TypeB(rows, cols); break;
- case "C": TypeC(rows, cols); break;
- case "D": TypeD(rows, cols); break;
- default: break;
- }
- }
- // Метод за тип А
- static void TypeA(int rows, int cols)
- {
- int[,] matrix = new int[rows, cols];
- int value = 1;
- // Два вложени цикъла, които пълнят матрицата със стойности
- for (int i = 0; i < cols; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- matrix[j, i] = value;
- value++;
- }
- }
- // Два вложени цикъла, които изчертават матрицата
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- Console.Write(matrix[i, j] + " ");
- }
- Console.WriteLine();
- }
- }
- // Тип B
- static void TypeB(int rows, int cols)
- {
- int[,] matrix = new int[rows, cols];
- int value = 1;
- for (int i = 0; i < cols; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- if (i % 2 == 0)
- {
- matrix[j, i] = value;
- value++;
- }
- else
- {
- matrix[rows - 1 - j, i] = value;
- value++;
- }
- }
- }
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- Console.Write(matrix[i, j] + " ");
- }
- Console.WriteLine();
- }
- }
- //Тип C
- static void TypeC(int rows, int cols)
- {
- int[,] matrix = new int[rows, cols];
- int value = 1;
- for (int i = rows - 1; i >= 0; i--)
- {
- int startR = i;
- for (int j = 0; j < rows - startR; j++)
- {
- if (j > cols - 1)
- {
- break;
- }
- matrix[startR + j, j] = value;
- value++;
- }
- }
- for (int i = 1; i < cols; i++)
- {
- int startC = i;
- for (int j = 0; j < cols - startC; j++)
- {
- if (j > rows - 1)
- {
- break;
- }
- matrix[j, startC + j] = value;
- value++;
- }
- }
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- Console.Write(matrix[i, j] + " ");
- }
- Console.WriteLine();
- }
- }
- // Тип D
- static void TypeD(int rows, int cols)
- {
- int[,] matrix = new int[rows, cols];
- int value = 1;
- int indexR = 0;
- int indexC = 0;
- while (value <= rows * cols)
- {
- matrix[indexR, indexC] = value;
- value++; ;
- bool canGoDown = (indexR + 1) < rows && matrix[indexR + 1, indexC] == 0 && !((indexC - 1) >= 0 && matrix[indexR, indexC - 1] == 0);
- if (canGoDown)
- {
- indexR++; continue;
- }
- bool canGoRight = (indexC + 1) < cols && matrix[indexR, indexC + 1] == 0;
- if (canGoRight)
- {
- indexC++; continue;
- }
- bool canGoUp = (indexR - 1) >= 0 && matrix[indexR - 1, indexC] == 0;
- if (canGoUp)
- {
- indexR--; continue;
- }
- bool canGoLeft = (indexC - 1) >= 0 && matrix[indexR, indexC - 1] == 0;
- if (canGoLeft)
- {
- indexC--; continue;
- }
- }
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- Console.Write(matrix[i, j] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment