Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MatrixD
- {
- static void Main()
- {
- Console.Write("Enter a size of the matrix: ");
- int n = int.Parse(Console.ReadLine());
- int[,] spiral = new int[n, n];
- int currentRow = 0;
- int currentColumn = 0;
- string direction = "down";
- for (int i = 1; i <= n*n; i++)
- {
- if (direction == "down" && (currentRow >= n || spiral[currentRow,currentColumn]!=0))
- {
- currentRow--;
- currentColumn++;
- direction = "right";
- }
- else if (direction == "right" && (currentColumn >= n || spiral[currentRow,currentColumn]!=0))
- {
- currentColumn--;
- currentRow++;
- direction = "up";
- }
- else if (direction == "up" && (currentRow > 0 || spiral[currentRow,currentColumn]!=0))
- {
- currentRow--;
- currentColumn--;
- direction = "left";
- }
- else if (direction == "left" && (currentColumn > 0 || spiral[currentRow,currentColumn]!=0))
- {
- currentColumn++;
- currentRow++;
- direction = "down";
- }
- spiral[currentRow, currentColumn] = i;
- if (direction == "down")
- {
- currentRow++;
- }
- else if (direction == "right")
- {
- currentColumn++;
- }
- else if (direction == "up")
- {
- currentRow--;
- }
- else if (direction == "left")
- {
- currentColumn--;
- }
- }
- for (int i=0; i<n; i++)
- {
- for (int j=0; j<n; j++)
- {
- Console.Write(spiral[i,j]+" ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement