Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SpiralMatrix
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[,] spiralMatrix = new int[n, n];
- int changerRight = 1;
- int changerDown = 1;
- int changerLeft = 0;
- int changerUp = 1;
- int rowRight = 0;
- int colRIght = 0;
- int movingNumber = 1;
- int rowDown = 1;
- int rowLeft = n - 1;
- int colLeft = n - 2;
- int rowUp = n - 2;
- bool whenToStop = true;
- do
- {
- whenToStop = false;
- while (colRIght < (n - changerRight) + 1)
- {
- spiralMatrix[rowRight, colRIght] = movingNumber;
- movingNumber++;
- colRIght++;
- whenToStop = true;
- }
- colRIght = changerRight;
- rowRight = changerRight;
- changerRight++;
- while (rowDown <= n - changerDown)
- {
- spiralMatrix[rowDown, n - changerDown] = movingNumber;
- movingNumber++;
- rowDown++;
- whenToStop = true;
- }
- changerDown++;
- rowDown = changerDown;
- while (colLeft >= n - (n - changerLeft))
- {
- spiralMatrix[rowLeft - changerLeft, colLeft] = movingNumber;
- movingNumber++;
- colLeft--;
- whenToStop = true;
- }
- changerLeft++;
- colLeft = (n - 2) - changerLeft;
- while (rowUp >= n - (n - changerUp))
- {
- spiralMatrix[rowUp, 0 + (changerUp - 1)] = movingNumber;
- movingNumber++;
- rowUp--;
- whenToStop = true;
- }
- rowUp = (n - 2) - changerUp;
- changerUp++;
- } while (whenToStop);
- for (int i = 0; i < n; i++)
- {
- for (int p = 0; p < n; p++)
- {
- Console.Write("{0, 6}", spiralMatrix[i, p]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement