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[,] array = new int[n, n];
- int left = 0;
- int right = n - 1;
- int top = 0;
- int bottom = n - 1;
- int number = 1;
- while (left <= right && top <= bottom)
- {
- for (int i = top, j = left; j <= right; ++j)
- {
- array[i, j] = number;
- ++number;
- }
- ++top;
- for (int i = top, j = right; i <= bottom; ++i)
- {
- array[i, j] = number;
- ++number;
- }
- --right;
- for (int i = bottom, j = right; j >= left; --j)
- {
- array[i, j] = number;
- ++number;
- }
- --bottom;
- for (int i = bottom, j = left; i >= top; --i)
- {
- array[i, j] = number;
- ++number;
- }
- ++left;
- }
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- Console.Write("{0,3} ", array[i, j]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement