Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _12.Fill_Matrix_A
- {
- class FillMatrixA
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n, n];
- FillSpral(matrix, n);
- PrintMatrix(matrix);
- }
- private static void FillSpral(int[,] matrix, int n)
- {
- int element = 0;
- int col = -1;
- int row = -1;
- while (element < n * n)
- {
- row++;
- col++;
- while (row < n && matrix[row, col] == 0)
- {
- element++;
- matrix[row, col] = element;
- row++;
- }
- row--;
- col++;
- while (col < n && matrix[row, col] == 0)
- {
- element++;
- matrix[row, col] = element;
- col++;
- }
- col--;
- row--;
- while (row >= 0 && matrix[row, col] == 0)
- {
- element++;
- matrix[row, col] = element;
- row--;
- }
- row++;
- col--;
- while (col >= 0 && matrix[row, col] == 0)
- {
- element++;
- matrix[row, col] = element;
- col--;
- }
- }
- }
- private static void PrintMatrix(int[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col].ToString().PadLeft(3) + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement