Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class PrintMatrix
- {
- static void Main()
- {
- int N = int.Parse(Console.ReadLine());
- int[,] matrix = new int[N, N];
- int row = 0, col = 0, counter = 1, circleSide = N, circle = 0;//тази променлива е важна
- for (int i = 0; i < N - 1; i++)
- {
- while(circleSide > 0)
- {
- matrix[row, col] = counter++;
- circleSide--;
- col++;
- }
- circle++;//4,2 (завъртания)
- circleSide = N - circle;
- col--;
- row++;
- while(circleSide > 0)
- {
- matrix[row, col] = counter++;
- circleSide--;
- row++;
- }
- circleSide = N - circle;
- row--;
- col--;
- while(circleSide > 0)
- {
- matrix[row, col] = counter++;
- circleSide--;
- col--;
- }
- circle++;//3,1
- circleSide = N - circle;
- row--;
- col++;
- while(circleSide > 0)
- {
- matrix[row,col] = counter++;
- circleSide--;
- row--;
- }
- circleSide = N - circle;
- row++;
- col++;
- }
- PrintResult(matrix, N);
- }
- private static void PrintResult(int[,] matrix,int N)
- {
- for (int rows = 0; rows < N; rows++)
- {
- for (int cols = 0; cols < N; cols++)
- {
- if(matrix[rows,cols] > 9) //този иф е заради правилното принтене
- {
- Console.Write(matrix[rows, cols] + " ");
- }
- else
- {
- Console.Write(matrix[rows, cols] + " ");
- }
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement