Advertisement
nadiahristova

Spiral Matrix

Dec 15th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 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.         string powNum = Convert.ToString(n * n);
  9.         int digitCounter = powNum.Length;
  10.         int numCounter = 1;
  11.         int plusOrMinusAxisDirection = 0;
  12.         sbyte direction = 1;
  13.         double i = n;
  14.         int x = 0;
  15.         int y = 0;
  16.         Console.Clear();
  17.  
  18.         do
  19.         {
  20.             for (int j = 1; j <= (int)i; j++)
  21.             {
  22.                 if (plusOrMinusAxisDirection == 2)
  23.                 {
  24.                     plusOrMinusAxisDirection = 0;
  25.                     direction = (sbyte)(direction * (-1));
  26.                 }
  27.  
  28.                 if (i % 1 == 0)
  29.                 {
  30.                     Console.SetCursorPosition(x, y);
  31.                     Console.Write(numCounter.ToString().PadRight(digitCounter, ' ') + " ");
  32.  
  33.                     if (j != (int)i)
  34.                     {
  35.                         x = x + direction * digitCounter + direction * 1;
  36.  
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     y = y + direction * 1;
  42.                     Console.SetCursorPosition(x, y);
  43.                     Console.Write(numCounter.ToString().PadRight(digitCounter, ' ') + " ");
  44.  
  45.                     if (j == (int)i)
  46.                     {
  47.                         x = x + (-1) * direction * digitCounter + (-1) * direction;
  48.                     }
  49.                 }
  50.                 numCounter++;
  51.             }
  52.             Console.WriteLine();
  53.             plusOrMinusAxisDirection++;
  54.             i -= 0.5;
  55.         } while (i >= 0);
  56.         Console.SetCursorPosition(0, Console.WindowHeight -1);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement