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