Advertisement
Icakis

SpiralMatrix With Loops Only

Apr 5th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2.  
  3. class SpiralMatrix
  4. {
  5.     static void Main()
  6.     {
  7.         Console.SetBufferSize(300, 400);
  8.         //Input
  9.         int n;
  10.         Console.Write("n=");
  11.         while (!int.TryParse(Console.ReadLine(), out n) || n < 1)
  12.         {
  13.             Console.WriteLine("Wrong input (integer n>=1)");
  14.             Console.Write("n=");
  15.         }
  16.         //Create SpiralMatrix
  17.         for (int i = 0; i < n; i++)
  18.         {
  19.             for (int j = 0; j < n; j++)
  20.             {
  21.                 int CurrentCircleI;
  22.                 int CurrentInCyrcleJ;
  23.                 int MinCircles;
  24.  
  25.                 if ((i + 1) <= (n + 1) / 2)
  26.                 {
  27.                     CurrentCircleI = i + 1;
  28.                     if ((j+1)<=(n+1)/2)
  29.                     {
  30.                         CurrentInCyrcleJ = j + 1;
  31.                     }
  32.                     else
  33.                     {
  34.                         CurrentInCyrcleJ = n - j;
  35.                     }
  36.                 }
  37.                 else
  38.                 {
  39.                     CurrentCircleI = n - i;
  40.                     if ((j + 1) <= (n + 1) / 2)
  41.                     {
  42.                         CurrentInCyrcleJ = j + 1;
  43.                     }
  44.                     else
  45.                     {
  46.                         CurrentInCyrcleJ = n - j;
  47.                     }
  48.                 }
  49.  
  50.                 MinCircles = Math.Min(CurrentCircleI, CurrentInCyrcleJ);
  51.                 int PrintNumber=-1;
  52.                 int CurrentCount = 1;
  53.                 int k = 0;
  54.                 for (int circle = 1; circle < MinCircles; circle++)
  55.                 {
  56.                     CurrentCount += 4 * (n - k) - 4;
  57.                     k += 2;
  58.                 }
  59.                 if (j >= i && i < n - j)
  60.                 {
  61.                     PrintNumber = CurrentCount + j - i; //PrintNumber = 0;
  62.                 }
  63.                 else if (j >= (n + 1) / 2 && j - i > 0)
  64.                 {
  65.                     PrintNumber = CurrentCount + n - k + i - MinCircles; //PrintNumber = 1;
  66.                 }
  67.                 else if (j <= i && i >= n - j)
  68.                 {
  69.                     PrintNumber = CurrentCount + i - j + 2 * (n - 2 * MinCircles) + 2; //PrintNumber = 2;
  70.                 }
  71.                 else if (j < i && i < n - j)
  72.                 {
  73.                     CurrentCount += 4 * (n - k) - 4;
  74.                     PrintNumber = CurrentCount - i + j; //PrintNumber = 3;
  75.                 }
  76.                 Console.Write("{0,5}", PrintNumber);
  77.             }
  78.             Console.WriteLine();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement