Advertisement
Hristo_B

SpiralNumbers

Jun 10th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.  
  9.         int[,] arraySpiral = new int[n, n];
  10.  
  11.         string direction = "right";
  12.  
  13.         int currentRow = 0;
  14.         int currentCol = 0;
  15.  
  16.         for (int i = 1; i <= n*n; i++)
  17.         {
  18.             if
  19.                 (direction == "right" && (currentCol >= n || (arraySpiral[currentRow, currentCol] != 0)))
  20.                
  21.             {
  22.                 currentCol--;
  23.                 currentRow++;
  24.                 direction = "down";
  25.                
  26.                
  27.             }
  28.             else if (direction == "down" && (currentRow >= n || (arraySpiral[currentRow, currentCol] != 0)))
  29.             {
  30.                 currentRow--;
  31.                 currentCol--;
  32.                 direction = "left";
  33.             }
  34.             else if (direction == "left" && (currentCol < 0 || (arraySpiral[currentRow, currentCol] != 0)))
  35.             {
  36.                 currentCol++;
  37.                 currentRow--;
  38.                 direction = "up";
  39.             }
  40.             else if (direction == "up" && (currentRow < 0 || (arraySpiral[currentRow, currentCol] != 0)))
  41.             {
  42.                 currentRow++;
  43.                 currentCol++;
  44.                 direction = "right";
  45.             }
  46.  
  47.             arraySpiral[currentRow, currentCol] = i;
  48.  
  49.             if (direction == "right")
  50.             {
  51.                 currentCol++;
  52.             }
  53.             else if (direction == "down")
  54.             {
  55.                     currentRow++;
  56.             }
  57.             else if (direction == "left")
  58.             {
  59.                 currentCol--;
  60.             }
  61.             else if (direction == "up")
  62.             {
  63.                 currentRow--;
  64.             }
  65.         }
  66.  
  67.         for (int i = 0; i < n; i++)
  68.         {
  69.             for (int j = 0; j < n; j++)
  70.             {
  71.                 Console.Write(arraySpiral[i,j] + "  ");
  72.             }
  73.             Console.WriteLine();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement