Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SpiralMatrix
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- int[,] spiral = new int[n, n];
- int count = 1;
- int row = 0;
- int col = -1;
- for (int k = 0; k <= n / 2; k++)
- {
- //right
- for (col = col + 1; col <= n - 1 - k; col++)
- {
- spiral[row, col] = count;
- if (count == n * n)
- goto print;
- count++;
- }
- col--;
- //down
- for (row = row + 1; row <= n - 1 - k; row++)
- {
- spiral[row, col] = count;
- if (count == n * n)
- goto print;
- count++;
- }
- row--;
- //left
- for (col = col - 1; col >= k; col--)
- {
- spiral[row, col] = count;
- if (count == n * n)
- goto print;
- count++;
- }
- col++;
- //up
- for (row = row - 1; row > k; row--)
- {
- spiral[row, col] = count;
- if (count == n * n)
- goto print;
- count++;
- }
- row++;
- }
- print:
- for (row = 0; row < n; row++)
- {
- for (col = 0; col < n; col++)
- {
- Console.Write("{0,4}", spiral[row, col]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement