Advertisement
Guest User

Untitled

a guest
Aug 17th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2.  
  3. class SpiralMatrix
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         int[,] array = new int[n, n];
  9.  
  10.         int left = 0;
  11.         int right = n - 1;
  12.         int top = 0;
  13.         int bottom = n - 1;
  14.         int number = 1;
  15.  
  16.         while (left <= right && top <= bottom)
  17.         {
  18.             for (int i = top, j = left; j <= right; ++j)
  19.             {
  20.                 array[i, j] = number;
  21.                 ++number;
  22.             }
  23.             ++top;
  24.  
  25.             for (int i = top, j = right; i <= bottom; ++i)
  26.             {
  27.                 array[i, j] = number;
  28.                 ++number;
  29.             }
  30.             --right;
  31.  
  32.             for (int i = bottom, j = right; j >= left; --j)
  33.             {
  34.                 array[i, j] = number;
  35.                 ++number;
  36.             }
  37.             --bottom;
  38.  
  39.             for (int i = bottom, j = left; i >= top; --i)
  40.             {
  41.                 array[i, j] = number;
  42.                 ++number;
  43.             }
  44.             ++left;
  45.         }
  46.  
  47.         for (int i = 0; i < n; ++i)
  48.         {
  49.             for (int j = 0; j < n; ++j)
  50.             {
  51.                 Console.Write("{0,3} ", array[i, j]);
  52.             }
  53.             Console.WriteLine();
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement