Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- string type = Console.ReadLine();
- int[,] matrix = new int[n, n];
- if (type == "a")
- {
- FillMatrixCaseA(matrix, n);
- }
- else if (type == "b")
- {
- FillMatrixCaseB(matrix, n);
- }
- else if (type == "c")
- {
- FillMatrixCaseC(matrix, n);
- }
- else if (type == "d")
- {
- FillMatrixCaseD(matrix, n);
- }
- PrintMatrix(matrix);
- }
- private static void PrintMatrix(int[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(0); j++)
- {
- Console.Write(matrix[i,j] + " ");
- }
- Console.WriteLine();
- }
- }
- private static void FillMatrixCaseD(int[,] matrix, int n)
- {
- int step = 1;
- int row = 0;
- int column = 0;
- while (row < matrix.GetLength(0))
- {
- matrix[row, column] = step;
- row++;
- step++;
- }
- row--;
- column++;
- while (column < matrix.GetLength(1))
- {
- matrix[row, column] = step;
- column++;
- step++;
- }
- row--;
- column--;
- while (row >= 0)
- {
- matrix[row, column] = step;
- step++;
- row--;
- }
- column--;
- row++;
- while (column >= 1)
- {
- matrix[row, column] = step;
- step++;
- column--;
- }
- column++;
- row++;
- while (step <= matrix.Length)
- {
- while (matrix[row, column] == 0)
- {
- matrix[row, column] = step;
- row++;
- step++;
- }
- if (step > matrix.Length)
- {
- break;
- }
- row--;
- column++;
- while (matrix[row, column] == 0)
- {
- matrix[row, column] = step;
- column++;
- step++;
- }
- if (step > matrix.Length)
- {
- break;
- }
- row--;
- column--;
- while (matrix[row, column] == 0)
- {
- matrix[row, column] = step;
- step++;
- row--;
- }
- if (step > matrix.Length)
- {
- break;
- }
- column--;
- row++;
- while (matrix[row, column] == 0)
- {
- matrix[row, column] = step;
- step++;
- column--;
- }
- if (step > matrix.Length)
- {
- break;
- }
- column++;
- row++;
- }
- }
- private static void FillMatrixCaseC(int[,] matrix, int n)
- {
- int row = n - 1;
- int col = 0;
- int count = 1;
- int num = 1;
- for (int i = n - 1; i >= 0; i--)
- {
- for (int j = 0; j < count; j++)
- {
- matrix[row, col] = num;
- row++;
- col++;
- num++;
- }
- row = i - 1;
- col = 0;
- count++;
- }
- row = 0;
- count = 0;
- for (int i = 0; i < n - 1; i++)
- {
- col = i + 1;
- for (int j = count; j < n - 1; j++)
- {
- matrix[row, col] = num;
- num++;
- row++;
- col++;
- }
- row = 0;
- count++;
- }
- }
- private static void FillMatrixCaseB(int[,] matrix, int n)
- {
- int count = 1;
- int row = 0;
- while (count <= n * n)
- {
- for (int i = row; i < row + 1; i++)
- {
- for (int j = 0; j < n; j++)
- {
- matrix[j, i] = count;
- count++;
- }
- }
- row++;
- if (count >= n * n)
- {
- break;
- }
- for (int i = row; i < row + 1; i++)
- {
- for (int j = n - 1; j >= 0; j--)
- {
- matrix[j, i] = count;
- count++;
- }
- }
- row++;
- }
- }
- private static void FillMatrixCaseA(int[,] matrix, int n)
- {
- int num = 1;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- matrix[j, i] = num;
- num++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement