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 Spiral_Matrix
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[,] matrx = new int[n, n];
- int rows = 0;
- int collumns = 0;
- string move = "right";
- int maxNumber = n * n;
- for (int i = 1; i <= maxNumber; i++)
- {
- if (move == "right" && (collumns > n - 1 || matrx[rows, collumns] != 0))
- {
- move = "down";
- collumns--;
- rows++;
- }
- if (move == "down" && (rows > n - 1 || matrx[rows, collumns] != 0))
- {
- rows--;
- collumns--;
- move = "left";
- }
- if (move == "left" && (collumns < 0 || matrx[rows, collumns] != 0))
- {
- move = "up";
- collumns++;
- rows--;
- }
- if (move == "up" && (rows < 0 || matrx[rows, collumns] != 0))
- {
- move = "right";
- rows++;
- collumns++;
- }
- matrx[rows, collumns] = i;
- if (move == "right")
- {
- collumns++;
- }
- if (move == "down")
- {
- rows++;
- }
- if (move == "left")
- {
- collumns--;
- }
- if (move == "up")
- {
- rows--;
- }
- }
- for (int x1 = 0; x1 < n; x1++)
- {
- for (int x2 = 0; x2 < n; x2++)
- {
- Console.Write("{0,4}", matrx[x1, x2] );
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement