Advertisement
ScorpS

Outputs Spiral

Dec 1st, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2.  
  3. class Spiral
  4. {
  5.     static void Main()
  6.     {
  7.         byte n = byte.Parse(Console.ReadLine());
  8.         int count = 1;
  9.         int row = 1;
  10.         int col = 0;
  11.         int stopper = n;
  12.         int stopperCol = 0;
  13.         int stopperRow = 3;
  14.         int special = n;
  15.         for (int k = 0; k < n; k++)
  16.         {
  17.             for (int i = 0; i < n * n; i++)
  18.             {
  19.  
  20.                 if (i < stopper)
  21.                 {
  22.                     Console.Write("{0,4}", count);
  23.                     count++;
  24.                 }
  25.                
  26.                 if ((i >= stopper) && ((i - stopper) < (stopper - 1)))
  27.                 {
  28.                     row += 2;
  29.                     col = special * 4 - 4;
  30.                     Console.SetCursorPosition(col, row);
  31.                     Console.Write("{0,4}", count);
  32.                     count++;
  33.                 }
  34.                
  35.                 if ((i - stopper) >= stopper - 1 && col > stopperCol)
  36.                 {
  37.                     col -= 4;
  38.                     Console.SetCursorPosition(col, row);
  39.                     Console.Write("{0,4}", count);
  40.                     count++;
  41.                 }
  42.                
  43.                 if (col == stopperCol && row > stopperRow)
  44.                 {
  45.                     row -= 2;
  46.                     Console.SetCursorPosition(col, row);
  47.                     Console.Write("{0,4}", count);
  48.                     count++;
  49.                 }
  50.             }
  51.             stopper -= 2;
  52.             stopperCol += 4;
  53.             stopperRow += 2;
  54.             special--;
  55.         }
  56.         Console.SetCursorPosition(0, n * 2 + 2);
  57.         Console.WriteLine();
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement