Advertisement
g-stoyanov

Exercise14Spiral v1.2

Dec 3rd, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2.  
  3. class Exercise14Spiral
  4. {
  5.     static void Main()
  6.     {
  7.         int input = int.Parse(Console.ReadLine());
  8.         int[,] matrix = new int[input, input];
  9.         int x = input - 1;
  10.         int y = 0;
  11.         int direction = 2;
  12.         int number = input;
  13.         for (int a = input; a > 0; a--)
  14.         {
  15.             matrix[0, input - a] = (input - a) + 1;
  16.             for (int b = 0; b < 2; b++)
  17.             {
  18.                 for (int c = 0; c < a - 1; c++)
  19.                 {
  20.                     switch (direction)
  21.                     {
  22.                         case 1: x++; break;
  23.                         case 2: y++; break;
  24.                         case 3: x--; break;
  25.                         case 4: y--; break;
  26.                     }
  27.                     number++;
  28.                     matrix[y, x] = number;
  29.                 }
  30.                 direction++;
  31.                 if (direction > 4)
  32.                 {
  33.                     direction = 1;
  34.                 }
  35.             }
  36.         }
  37.         for (int matrixY = 0; matrixY < input; matrixY++)
  38.         {
  39.             for (int matrixX = 0; matrixX < input; matrixX++)
  40.             {
  41.                 Console.Write(Convert.ToString(matrix[matrixY, matrixX]).PadLeft((Convert.ToString(input * input).Length + 1), ' '));
  42.             }
  43.             Console.WriteLine(Environment.NewLine);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement